由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - What is the difference between class and struct?
相关主题
typedefc++ initialize struct
forward declaration一道面试怪题C++. (转载)
class D:public B;问个copy constructor的问题
C++怎样设置全局变量C++ class cross reference problem
spring di的project问一个C++的面试问题
about struct and class in c++[合集] (c++)为什么不能把这个function的definition放到class里
[合集] Interview Question[合集] 请教: 模板类之继承的一个小问题
C++ Q16: dereferencing请教一个MS Linked List的问题
相关话题的讨论汇总
话题: struct话题: class话题: what话题: public话题: difference
进入Programming版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
第一个人的回答:
1. Struct has public access by default (class has private access).
2. When you are going to inherit from that class.
第一个人的第一点我明白。但第二点也是谈public/private的问题吗?
第二个人的回答:
Another difference between struct and class can be shown if you try to
declare struct MyClass; or class MyStruct; Both keywords are not
interchangeable in declarations, not just for class/struct definitions.
第二个人的回答是什么意思?
Source:
http://stackoverflow.com/questions/50447/favorite-c-interview-q
A**u
发帖数: 2458
2
第一个人
2。struct所有成员都是public,继承时子类可以访问父类所有成员,和1类似
第2个人
你用class 定义类,那么只能用class声明,
你用struct定义类,那么只能用struct声明

【在 c**********e 的大作中提到】
: 第一个人的回答:
: 1. Struct has public access by default (class has private access).
: 2. When you are going to inherit from that class.
: 第一个人的第一点我明白。但第二点也是谈public/private的问题吗?
: 第二个人的回答:
: Another difference between struct and class can be shown if you try to
: declare struct MyClass; or class MyStruct; Both keywords are not
: interchangeable in declarations, not just for class/struct definitions.
: 第二个人的回答是什么意思?
: Source:

c**********e
发帖数: 2007
3
第二个人说得好像是废话。两个显然不可替代使用。
c**********e
发帖数: 2007
4
谢谢奥古大牛。

【在 A**u 的大作中提到】
: 第一个人
: 2。struct所有成员都是public,继承时子类可以访问父类所有成员,和1类似
: 第2个人
: 你用class 定义类,那么只能用class声明,
: 你用struct定义类,那么只能用struct声明

l*********s
发帖数: 5409
5
mark
f*******n
发帖数: 12623
6
The second person might be confused with something from C. In C, to declare
a variable of a struct type, you have to write "struct MyStruct foo;" unless
you define a typedef. So the keyword "struct" is necessary in the
declaration. However, in C++, "struct" and "class" declare new types
directly, so there is no need to use "struct" or "class" in a declaration.
X****r
发帖数: 3557
7
You need something like "struct X;" or "class X;" in forward declaration.
You also need this struct/class keyword along with the struct/class name
(called elaborated type specifier) if you want to specify a type name
shadowed by a function or variable name, e.g.
struct X {
int a;
};
int X() {
return 0;
}
int g = X(); // calls the function
// The 'struct' keyword is required here.
struct X x;

declare
unless

【在 f*******n 的大作中提到】
: The second person might be confused with something from C. In C, to declare
: a variable of a struct type, you have to write "struct MyStruct foo;" unless
: you define a typedef. So the keyword "struct" is necessary in the
: declaration. However, in C++, "struct" and "class" declare new types
: directly, so there is no need to use "struct" or "class" in a declaration.

X****r
发帖数: 3557
8
For #2, note that this means the struct by default has public base
class(es). It does not mean the struct is a public base class by default
itself. e.g.
class X {};
struct Y : /* public */ X {};
versus
struct A {};
class B : /* private */ A {};

【在 A**u 的大作中提到】
: 第一个人
: 2。struct所有成员都是public,继承时子类可以访问父类所有成员,和1类似
: 第2个人
: 你用class 定义类,那么只能用class声明,
: 你用struct定义类,那么只能用struct声明

A**u
发帖数: 2458
9
谢谢指出.

【在 X****r 的大作中提到】
: For #2, note that this means the struct by default has public base
: class(es). It does not mean the struct is a public base class by default
: itself. e.g.
: class X {};
: struct Y : /* public */ X {};
: versus
: struct A {};
: class B : /* private */ A {};

1 (共1页)
进入Programming版参与讨论
相关主题
请教一个MS Linked List的问题spring di的project
Any better way to declare a function?about struct and class in c++
C++ Template Question[合集] Interview Question
recursive template?C++ Q16: dereferencing
typedefc++ initialize struct
forward declaration一道面试怪题C++. (转载)
class D:public B;问个copy constructor的问题
C++怎样设置全局变量C++ class cross reference problem
相关话题的讨论汇总
话题: struct话题: class话题: what话题: public话题: difference