马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
class Person{
char *name; int id;
public:
Person(const char* pn="noName", int i = 0){
name=new char[strlen(pn)+1];
if(name!=0) strcpy(name,pn);
id = i;
}
~Person(){
cout<<"Destruct "<<name<<endl;
delete [] name;
}
void print(){
cout<<name<<" : "<<id<<'\n';
}
void copy(Person const &rp){
delete [] name;
name=new char[strlen(rp.name)+1];
if(name!=0) strcpy(name,rp.name);
id = rp.id;
}
};
int main(){
Person p1("Randy", 1),p2;
p2.copy(p1).print();//这两行我是真的没懂
p2.copy(p2).print();
}
|