#include<iostream>
#include<string.h>
using namespace std;
class name{
public:
name(char *pn);
~name();
name::name(const name& n);
protected:
char *pname;
int size;
};
name::name(char *pn){
cout << "constructing" << pn << endl;
pname = new char[strlen(pn) + 1];
if (pname != 0)
strcpy_s(pname, strlen(pn) + 1, pn);
size = strlen(pn);
}
name::~name(){
cout << "Destructing" << pname << endl;
pname[0] = '\0';
delete[]pname;
size = 0;
}
name::name(const name& n){//拷贝构造函数。
cout << "call copy_constructor" << endl;
int size = n.size+1;
pname = new char[size ];//为其临时对象申请个内存。让临时对象的指针指向它。
//防止释放临时对象时把原来的对象的指针所指空间给弄乱
strcpy_s(pname, size,n.pname);//两个参数不好办,用三个参数。
}
void main(){
name Obj1("NoName");
cout << "strlen(\"NoName\")=" << strlen("NoName") << endl;
name Obj2 = Obj1;
system("pause");
}