copy构造函数实现对象的深拷贝
#include<stdio.h>
#include<stdlib.h>#include<string.h>
class A{
private:
char *p;
public:
A(const A &a)
{
p=(char *)malloc(sizeof(10));
memset(p,0,sizeof(p));
memcpy(p,a.p,sizeof(a.p));
}
A(char *p1)
{
p=(char *)malloc(sizeof(10));
memset(p,0,sizeof(p));
memcpy(p,p1,sizeof(p1));
}
char *getP()
{
return p;
}
~A()
{
if(p!=NULL)
{
free(p);
p=NULL;
}
}
};
void main()
{
char s[]="abc";
A myA(s);
A myB=myA;
printf("%s %s",myA.getP(),myB.getP());
}