#include<stdio.h>classSTU{public:STU(){printf("STU\n");}STU(int id){printf("STU(int id)\n");}~STU(){printf("STU Bye!!!\n");}};intmain(int argc,charconst*argv[]){STU a;//调用无参构造函数STU b(5);//显调用STU c =10;//隐式调用STU d = c;//调用拷贝构造函数STU *p =newSTU(1000);//堆中申请return0;}
深浅拷贝问题(快捷方式与复制)
浅拷贝
#include<stdio.h>classSTU{public:STU(){printf("STU\n");p =newchar[10];}~STU(){printf("STU Bye!!!\n");delete[] p;}private:char*p;};intmain(int argc,charconst*argv[]){STU a;//调用无参构造函数STU b = a;return0;}
深拷贝
#include<stdio.h>#include<string.h>classSTU{public:STU(){printf("STU\n");p =newchar[10];strcpy(p,"hello!!!");}STU(const STU &a){//拷贝构造函数printf("const STU &a\n");p =newchar[10];strcpy(p, a.p);//拷贝}~STU(){printf("STU Bye!!!\n");delete[] p;}private:char*p;};intmain(int argc,charconst*argv[]){STU a;//调用无参构造函数STU b = a;return0;}