-3

For example, if i have a class like this;

#import "B.h"
class A
{
    B object;
};

would B's constructor get called when I created a A object?

4

4 回答 4

3

Yes, the default constructor will be called (if present) or a compiler error will be triggered if there is no default constructor for B.

于 2012-04-17T16:40:08.120 回答
2

Yes. One way to test this is to put some simple cout calls in the constructors to watch the constructor calls happen.

于 2012-04-17T16:39:45.257 回答
0

Yes, member constructor is called in order of declaration for the containing class.

于 2012-04-17T16:49:47.630 回答
0

One should mention that this rule apply both ways, calling class 'A's destructor calls its members default destructor's.

于 2012-04-17T17:30:08.173 回答