c*********n 发帖数: 128 | 1 如果有一个class
class Apple {
Feed feed;
}
(注意:并没有自己定义constructor,当然也就没有手工给feed分配内存)
是不是如下语句
Apple apple;
就已经给apple 和 apple.feed分配内存了? (只是内存内容不可预料?)
并不是像java那样 Apple apple只是定义一个值为null的reference? |
h**o 发帖数: 347 | 2 feed的内容是Feed()
如果没有定义Feed();会报错
【在 c*********n 的大作中提到】 : 如果有一个class : class Apple { : Feed feed; : } : (注意:并没有自己定义constructor,当然也就没有手工给feed分配内存) : 是不是如下语句 : Apple apple; : 就已经给apple 和 apple.feed分配内存了? (只是内存内容不可预料?) : 并不是像java那样 Apple apple只是定义一个值为null的reference?
|
c*********n 发帖数: 128 | 3 just tested the following code. it compiled.
#include
using namespace std;
class Feed{};
class Apple{
public:
Feed feed;
};
int main() {
Apple apple;
cout << (long) &apple << endl;
cout << (long) &apple.feed << endl;
}
$ ./a.out
140735076833951
140735076833951
【在 h**o 的大作中提到】 : feed的内容是Feed() : 如果没有定义Feed();会报错
|
O*******d 发帖数: 20343 | 4 在java里,stack上只有pointer and primitive data type。 所以在java里,数列要
用new, 数列的每个成员也要用new. 而在C/C++里, stack上可以有各种data type,
包括class instance 和数列。 |