2
我有下面的代码,我不明白,为什么在创建对象B,类A的构造函数不称为.......我将非常感谢您的一点help.hank。C++类组成,为什么构造函数不被调用?
#include <iostream>
using namespace std;
class MyClass
{
int x;
public:
MyClass(int y);
MyClass(MyClass &my)
{
x = my.x;
cout << "My class created by copy" << endl;
}
};
MyClass::MyClass(int y)
{
x = y;
cout << "My class created" << endl;
}
class A {
MyClass k;
public:
A(MyClass &my) : k(my) {
cout << "A created" << endl;
}
};
class B {
A data;
public:
B(A& aa) : data(aa)
{
cout << "B created" << endl;
}
};
int main()
{
MyClass obj(100);
A a(obj);
B b(a);
return 0;
}
执行:
My class created
My class created by copy
A created
My Class created by copy
B created
'**'在代码视图中标出东西不适用于标记。请编辑! –
为什么downvotes家伙?新用户。欢迎来到SE。 –
为什么downvote? **我真的很讨厌** [tag:C++]相关问题的积极downvoting,没有留下任何评论(至少在这里我已经注意到了)。给一些额外的标签似乎可以放松这种行为,这可能适用于第一次或低信誉的OP。给他们一个机会的人!尝试帮助! –