class Person
{
public:char*name;Person(constchar*s){
name = new char[strlen(s)+1];strcpy(name, s);};~Person(){
delete[] name;}};intmain(){
Person p1("Joh");
Person p2 = p1;//member-member wise,成员指针name指向同一块内存。delete时会出错printf("p1.name=%p\n",p1.name);printf("p2.name=%p",p2.name);return0;}
拷贝构造的调用时刻 (1)初始化; (2)函数以对象为参数(3)返回类型为对象
返回类型为对象:
Person f(){returnPerson("John");}intmain(){
Person p=f();//(1) Person @temp=f(); Person=@temp;//or (2) Person p=Person("John")return0;}
class A
{
private:staticint i;//declation
public:A(){i=10;};voidsay(int ii){cout<<this->i<<endl;}};int A::i =10;//静态成员变量只能在定义式初始化,不能在initialist 中初始化//static int A::i=10; //ERROR ,因为对象的成员变量可以在任何文件进行访问,static修饰后,表示只能在该文件访问intmain(){
A a;
a.say(2);return0;}