#include<iostream> #include<string.h> usingnamespacestd; classstr { private: char *st; public: str(char *a) { set(a); cout<<"构造函数:str addr "<<this<<" st addr "<<&st<<" st point to "<<(void *)st<<" st "<<st<<endl; } str & operator==(str a) {//二元操作符“==”,参数类型和后者一致 cout<<"op:形参 str a addr "<<&a<<" a.st addr "<<&(a.st)<<" a.st point to "<<(void *)(a.st)<<" a.st content "<<(a.st)<<endl; delete st; set(a.st); return *this; } str & operator=(str const & a) {//二元操作符“=”,参数类型和后者一致 //str & operator=(str & a) {//二元操作符“=”,参数类型和后者一致 cout<<"op:形参 str const & a addr "<<&a<<" a.st addr "<<&(a.st)<<" a.st point to "<<(void *)(a.st)<<" a.st content "<<(a.st)<<endl; delete st; set(a.st); return *this; }
voidshow(){cout<<"show func: "<<st<<endl;} ~str(){ cout<<"~str:before str addr "<<this<<" st addr "<<&st<<" st point to "<<(void *)(st)<<" st content "<<st<<endl; delete st; //cout<<"~str:after str addr "<<this<<" st addr "<<&st<<" st point to "<<(void *)(st)<<" st content "<<st<<endl; } voidset(char *s)//初始化st { cout<<"set:before new st addr "<<&st<<" st point to "<<(void *)(st)<<endl; st = newchar[strlen(s)+1];//先分配空间,否则野指针,+1存"\0" cout<<"set:after new st addr "<<&st<<" st point to "<<(void *)(st)<<endl; if(st) strcpy(st,s); } };
set:before new st addr 0xbfce5374 st point to 0xb785c680 set:after new st addr 0xbfce5374 st point to 0x9bffa10 构造函数:str addr 0xbfce5374 st addr 0xbfce5374 st point to 0x9bffa10 st he set:before new st addr 0xbfce5378 st point to 0x804abc4 set:after new st addr 0xbfce5378 st point to 0x9bffa20 构造函数:str addr 0xbfce5378 st addr 0xbfce5378 st point to 0x9bffa20 st she show func: he show func: she op:形参 str a addr 0xbfce537c a.st addr 0xbfce537c a.st point to 0x9bffa10 a.st content he set:before new st addr 0xbfce5378 st point to 0x9bffa20 set:after new st addr 0xbfce5378 st point to 0x9bffa20 ~str:before str addr 0xbfce537c st addr 0xbfce537c st point to 0x9bffa10 st content he show func: show func: he ~str:before str addr 0xbfce5378 st addr 0xbfce5378 st point to 0x9bffa20 st content he ~str:before str addr 0xbfce5374 st addr 0xbfce5374 st point to 0x9bffa10 st content
注意第二次两行show之前的那句~str:before str addr 0xbfce537c st addr 0xbfce537c st point to 0x9bffa10 st content he
set:before new st addr 0xbff47b38 st point to 0x804aba0 set:after new st addr 0xbff47b38 st point to 0x9693a10 构造函数:str addr 0xbff47b38 st addr 0xbff47b38 st point to 0x9693a10 st he set:before new st addr 0xbff47b3c st point to 0x8049302 set:after new st addr 0xbff47b3c st point to 0x9693a20 构造函数:str addr 0xbff47b3c st addr 0xbff47b3c st point to 0x9693a20 st she show func: he show func: she op:形参 str const & a addr 0xbff47b38 a.st addr 0xbff47b38 a.st point to 0x9693a10 a.st content he set:before new st addr 0xbff47b3c st point to 0x9693a20 set:after new st addr 0xbff47b3c st point to 0x9693a20 show func: he show func: he ~str:before str addr 0xbff47b3c st addr 0xbff47b3c st point to 0x9693a20 st content he ~str:before str addr 0xbff47b38 st addr 0xbff47b38 st point to 0x9693a10 st content he