原题:mooc北大的c++国家精品课​​http://cxsjsxmooc.openjudge.cn/2019t3summerall/011/​

#include <iostream>
#include <string>
using namespace std;
class Base {
public:
int k;
Base(int n):k(n) { };
};
class Big
{
public:
int v;
Base b;
Big(int init_n):b(init_n),v(init_n){ //构造函数
}
Big(const Big& init_c) //这里这样定义复制构造函数是不对的
{
v=init_c.v;
b=init_c.b;
}


};
int main()
{
int n;
while(cin >>n) {
Big a1(n);
Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
}
}

错误如上,

正确代码如下:

#include <iostream>
#include <string>
using namespace std;
class Base {
public:
int k;
Base(int n):k(n) { };
};
class Big
{
public:
int v;
Base b;
Big(int init_n):b(init_n),v(init_n){
}
Big(const Big& init_c):v(init_c.v),b(init_c.b) //这样构造复制构造函数是正确的
{
}


};
int main()
{
int n;
while(cin >>n) {
Big a1(n);
Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
}
}