#include<iostream>
#include<cstring>
using namespace std;
class A
{
private:
char *a;
public:
A(char *aa)
{
a = new char[strlen(aa)+1];//(1)这样处理的意义在于:\0要占用空间
strcpy(a,aa);//(2)数据成员a与形式参数aa的关系:将aa的值复制给a
}
A(A &b)
{
a = new char[strlen(b.a)+1];
strcpy(a,b.a);
}
~A()
{
delete []a;//(3)这样处理的意义在于:释放空间
}
void output()
{
cout<<a<<endl;
}
};
int main(){
A a("good morning, code monkeys!");
a.output();
A b(a);
b.output();
return 0;
}
运行结果: