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 | |
c**********e 发帖数: 2007 | 4 谢谢奥古大牛。
【在 A**u 的大作中提到】 : 第一个人 : 2。struct所有成员都是public,继承时子类可以访问父类所有成员,和1类似 : 第2个人 : 你用class 定义类,那么只能用class声明, : 你用struct定义类,那么只能用struct声明
|
l*********s 发帖数: 5409 | |
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 {};
|