我试过这个:
class cls1{
public:
cls1(){ cout << "cls1 constructor\n";}
cls1 (cls1 & o){ cout << "cls1 copy constructor\n";}
};
class cls2{
public:
cls2 () { cout << "cls2 constructor\n";}
cls2 (cls2 & o){ cout << "cls2 copy constructor\n";}
};
class my_cls{
public:
cls1 o1;
cls2 o2;
my_cls(){ cout << "my_cls constructor\n";}
my_cls(my_cls& o){ cout << "my_cls copy constructor\n";}
};
void f(my_cls o){}
int main(){
my_cls p;
f(p);
return 0;
}
但输出是:
cls1 constructor
cls2 constructor
my_cls constructor
cls1 constructor
cls2 constructor
my_cls copy constructor
我觉得这很令人困惑,因为有人告诉我,对于每个成员,复制构造函数调用该成员的复制构造函数,对预置类型执行 execpt,当逐位复制时。(我希望在 my_cls 的复制构造函数之前调用 cls1 和 cls2 的复制构造函数)