分类: C/C++
2010-11-11 16:32:19

~A()
{
delete name;
}
class A
{
public:
A()
{
}
A(int id,char *t_name)
{
_id=id;
name=new char[strlen(t_name)+1];
strcpy(name,t_name);
}
A& operator =(A& a)
{
this->_id=a._id;
int len=strlen(a.name);
name=new char[len+1];
strcpy(name,a.name);
return *this;
}
~A()
{
cout<<"~destructor"<<endl;
delete name;
}
int _id;
char *name;
};其内存分配如下:
references:
类的深拷贝函数的重载
public class A
{
public:
...
A(A &a);//重载拷贝函数
A& operator=(A &b);//重载赋值函数
//或者 我们也可以这样重载赋值运算符 void operator=(A &a);即不返回任何值。如果这样的话,他将不支持客户代买中的链式赋值 ,例如a=b=c will be prohibited!
private:
int _id;
char *username;
}
A::A(A &a)
{
_id=a._id;
username=new char[strlen(a.username)+1];
if(username!=NULL)
strcpy(username,a.usernam);
}
A& A::operaton=(A &a)
{
if(this==&a)// 问:什么需要判断这个条件?(不是必须,只是优化而已)。答案:提示:考虑a=a这样的操作。
return *this;
if(username!=NULL)
delete username;
_id=a._id;
username=new char[strlen(a.username)+1];
if(username!=NULL)
strcpy(username,a.usernam);
return *this;
}
//另外一种写法:
void A::operation=(A &a)
{
if(username!=NULL)
delete username;
_id=a._id;
username=new char[strlen(a.username)+1];
if(username!=NULL)
strcpy(username,a.usernam);
}
其实,从上可以看出,赋值运算符和拷贝函数很相似。只不过赋值函数最好有返回值(进行链式赋值),返回也最好是对象的引用(为什么不是对象本身呢?note2有讲解), 而拷贝函数不需要返回任何。同时,赋值函数首先要释放掉对象自身的堆空间(如果需要的话),然后进行其他的operation.而拷贝函数不需要如此,因为对象此时还没有分配堆空间。
// virtual.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string.h"
#include "stdlib.h"
#include "assert.h"
class complex
{
public:
int real;
int virt;
public:
complex(){real=virt=0;}
complex(int treal,int tvirt){real=treal;virt=tvirt;}
complex operator+(const complex &x)
{
real+=x.real;
virt+=x.virt;
return *this;
}
complex operator=(const complex &x)
{
return complex(x.real,x.virt);
}
};

class A
{
public:
A(){m_username=NULL;printf("null constructor");}
A(char *username)
{
int len;
len=strlen(username);
m_username=new char[len+1];//(char*)malloc(sizeof(len+1));
strcpy(m_username,username);
printf("\nUsername is %s\n",m_username);
}
A(A &a);
A operator=(A &b);
int test(const int &x)
{
return x;
}
virtual ~A()
{
// if(m_username)
{
delete m_username;
printf("\nA is destructed\n");
}
}


protected:
char *m_username;
};


A::A(A &a)
{
int len=strlen(a.m_username);
this->m_username=new char[len+2];
strcpy(m_username,a.m_username);
strcat(m_username,"f");
printf("\ndeep copy function");
}

A A::operator=(A &b)
{
if(m_username)
delete m_username;
int len=strlen(b.m_username);
this->m_username=new char[len+1];
strcpy(m_username,b.m_username);
// printf("copied successfully!");
return *this;
}

class B:public A
{
public:
B(char *username,char *password):A(username)
{
int len=strlen(password)+1;
m_password=new char[len];//(char *)malloc(sizeof(len));
strcpy(m_password,password);
printf("username:%s,password:%s\n",m_username,m_password);
}
~B()
{
delete m_password;
printf("B is destructed\n");
}
protected:
char *m_password;
};
int main(int argc, char* argv[])
{
// B b("herengang","982135");
// A *a=&b;
// delete a;
A a("haha");
A b;
printf("\nbegin to invoke copy function");
b=a;
// printf("%d",b.test(2));
//complex x(1,3),y(1,4);
//x=(x+y);
//printf("%d,%d",x.real,x.virt);
return 0;

}

1 重载赋值运算符返回结果为类对象的运行结果