为类A增加拷贝构造函数
#include<iostream>
#include<string.h>
using namespace std;
class A
{
private:
char *a;
public:
A(char *aa)
{
a=new char[strlen(aa)+1];
strcpy(a,aa);
}
~A()
{
delete []a;
}
A(const A&r)
{
a=new char[strlen(r.a)+1];
strcpy(a,r.a);
}
void output()
{
cout<<a<<endl;
}
};
int main()
{
A a("good morning, code monkeys!");
a.output();
A b(a);
b.output();
return 0;
}
运行结果: