重载=时返回对象引用与非引用的区别
2014-02-17 20:27
281 查看
摘要:
在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作。
如果对象在申明的同时马上进行初始化操作,则称之为拷贝运算。例如:
class1 A("af"); class1 B=A;
此时其实际调用的是B(A)这样的浅拷贝操作。
如果对象在申明之后,再进行赋值运算,我们称之为赋值运算。例如:
class1 A("af"); class1 B;
B=A;
此时实际调用的类的缺省赋值函数B.operator=(A);
不管是浅拷贝还是赋值运算,其都有缺省的定义。也就是说,即使我们不overload这两种operation,仍然可以运行。
那么,我们到底需不需要overload这两种operation 呢?
答案就是:一般,我们我们需要手动编写析构函数的类,都需要overload 拷贝函数和赋值运算符。
下面介绍类的赋值运算符
1.C++中对象的内存分配方式
在C++中,对象的实例在编译的时候,就需要为其分配内存大小,因此,系统都是在stack上为其分配内存的。这一点和C#完全不同!千万记住:在C#中,所有类都是reference type,要创建类的实体,必须通过new在heap上为其分配空间,同时返回在stack上指向其地址的reference.
因此,在C++中,只要申明该实例,在程序编译后,就要为其分配相应的内存空间,至于实体内的各个域的值,就由其构造函数决定了。
例如:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class A
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A(int id,char *t_name)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_id=id;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
name=new char[strlen(t_name)+1];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(name,t_name);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
private:
char *name;
int _id;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A a(1,"herengang");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A b;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
在程序编译之后,a和b在stack上都被分配相应的内存大小。只不过对象a的域都被初始化,而b则都为随机值。
其内存分配如下:
![](http://images.cnblogs.com/cnblogs_com/winston/copy.JPG)
2. 缺省情况下的赋值运算符
如果我们执行以下:
b=a;
则其执行的是缺省定义的赋值运算。所谓缺省的赋值运算,是指对象中的所有位于stack中的域,进行相应的复制。但是,如果对象有位于heap上的域的话,其不会为拷贝对象分配heap上的空间,而只是指向相同的heap上的同一个地址。
执行b=a这样的缺省的赋值运算后,其内存分配如下:
![](http://images.cnblogs.com/cnblogs_com/winston/copy1.JPG)
因此,对于缺省的赋值运算,如果对象域内没有heap上的空间,其不会产生任何问题。但是,如果对象域内需要申请heap上的空间,那么在析构对象的时候,就会连续两次释放heap上的同一块内存区域,从而导致异常。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
~A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete name;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
3.解决办法--重载(overload)赋值运算符
因此,对于对象的域在heap上分配内存的情况,我们必须重载赋值运算符。当对象间进行拷贝的时候,我们必须让不同对象的成员域指向其不同的heap地址--如果成员域属于heap的话。
因此,重载赋值运算符后的代码如下:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class A
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A(int id,char *
16def
t_name)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_id=id;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
name=new char[strlen(t_name)+1];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(name,t_name);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A& operator =(A& a)
//注意:此处一定要返回对象的引用,否则返回后其值立即消失!
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(name!=NULL)
delete name;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this->_id=a._id;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len=strlen(a.name);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
name=new char[len+1];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(name,a.name);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return *this;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
~A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cout<<"~destructor"<<endl;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete name;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int _id;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char *name;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
int main()
{
A a(1,"herengang");
A b;
b=a;
}
其内存分配如下:
![](http://images.cnblogs.com/cnblogs_com/winston/copy2.JPG)
这样,在对象a,b退出相应的作用域,其调用相应的析构函数,然后释放分别属于不同heap空间的内存,程序正常结束。
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.username);
}
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.而拷贝函数不需要如此,因为对象此时还没有分配堆空间。
note1:
不要按值向函数传递对象。如果对象有内部指针指向动态分配的堆内存,丝毫不要考虑把对象按值传递给函数,要按引用传递。并记住:若函数不能改变参数对象的状态和目标对象的状态,则要使用const修饰符
note2:问题:
对于类的成员需要动态申请堆空间的类的对象,大家都知道,我们都最好要overload其赋值函数和拷贝函数。拷贝构造函数是没有任何返回类型的,这点毋庸置疑。 而赋值函数可以返回多种类型,例如以上讲的void,类本身class1,以及类的引用 class &? 问,这几种赋值函数的返回各有什么异同?
答:1 如果赋值函数返回的是void ,我们知道,其唯一一点需要注意的是,其不支持链式赋值运算,即a=b=c这样是不允许的!
2 对于返回的是类对象本身,还是类对象的引用,其有着本质的区别!
第一:如果其返回的是类对象本身。
A operator =(A& a)
{
if(name!=NULL)
delete name;
this->_id=a._id;
int len=strlen(a.name);
name=new char[len+1];
strcpy(name,a.name);
return *this;
}
其过程是这样的:
class1 A("herengnag");
class1 B;
B=A;
看似简单的赋值操作,其所有的过程如下:
1 释放对象原来的堆资源
2 重新申请堆空间
3 拷贝源的值到对象的堆空间的值
4 创建临时对象(调用临时对象拷贝构造函数),将临时对象返回
5. 临时对象结束,调用临时对象析构函数,释放临时对象堆内存
my god,还真复杂!!
但是,在这些步骤里面,如果第4步,我们没有overload 拷贝函数,也就是没有进行深拷贝。那么在进行第5步释放临时对象的heap 空间时,将释放掉的是和目标对象同一块的heap空间。这样当目标对象B作用域结束调用析构函数时,就会产生错误!!
因此,如果赋值运算符返回的是类对象本身,那么一定要overload 类的拷贝函数(进行深拷贝)!
第二:如果赋值运算符返回的是对象的引用,
A& operator =(A& a)
{
if(name!=NULL)
delete name;
this->_id=a._id;
int len=strlen(a.name);
name=new char[len+1];
strcpy(name,a.name);
return *this;
}
那么其过程如下:
1 释放掉原来对象所占有的堆空间
1.申请一块新的堆内存
2 将源对象的堆内存的值copy给新的堆内存
3 返回源对象的引用
4 结束。
因此,如果赋值运算符返回的是对象引用,那么其不会调用类的拷贝构造函数,这是问题的关键所在!!
完整代码如下:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// virtual.cpp : Defines the entry point for the console application.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "string.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "stdlib.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "assert.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class complex
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int real;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int virt;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
complex(){real=virt=0;}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
complex(int treal,int tvirt){real=treal;virt=tvirt;}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
complex operator+(const complex &x)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
real+=x.real;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
virt+=x.virt;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return *this;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
complex operator=(const complex &x)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return complex(x.real,x.virt);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class A
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
A(){m_username=NULL;printf("null constructor");}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A(char *username)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
len=strlen(username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_username=new char[len+1];//(char*)malloc(sizeof(len+1));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(m_username,username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("\nUsername is %s\n",m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A(A &a);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A operator=(A &b);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int test(const int &x)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return x;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
virtual ~A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// if(m_username)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete m_username;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("\nA is destructed\n");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char *m_username;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
A::A(A &a)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len=strlen(a.m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this->m_username=new char[len+2];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(m_username,a.m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcat(m_username,"f");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("\ndeep copy function");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
A A::operator=(A &b)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(m_username)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete m_username;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len=strlen(b.m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this->m_username=new char[len+1];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(m_username,b.m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// printf("copied successfully!");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return *this;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class B:public A
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
B(char *username,char *password):A(username)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len=strlen(password)+1;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_password=new char[len];//(char *)malloc(sizeof(len));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(m_password,password);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("username:%s,password:%s\n",m_username,m_password);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
~B()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete m_password;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("B is destructed\n");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char *m_password;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// B b("herengang","982135");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// A *a=&b;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// delete a;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A a("haha");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A b;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("\nbegin to invoke copy function");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
b=a;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// printf("%d",b.test(2));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//complex x(1,3),y(1,4);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//x=(x+y);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//printf("%d,%d",x.real,x.virt);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return 0;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1 重载赋值运算符返回结果为类对象的运行结果
![](http://images.cnblogs.com/cnblogs_com/winston/1.GIF)
明显, 运算符最后调用了拷贝构造函数
2 重载赋值运算符返回结果为类对象引用的运行结果
![](http://images.cnblogs.com/cnblogs_com/winston/2.GIF)
很明显,没有调用拷贝构造函数
在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作。
如果对象在申明的同时马上进行初始化操作,则称之为拷贝运算。例如:
class1 A("af"); class1 B=A;
此时其实际调用的是B(A)这样的浅拷贝操作。
如果对象在申明之后,再进行赋值运算,我们称之为赋值运算。例如:
class1 A("af"); class1 B;
B=A;
此时实际调用的类的缺省赋值函数B.operator=(A);
不管是浅拷贝还是赋值运算,其都有缺省的定义。也就是说,即使我们不overload这两种operation,仍然可以运行。
那么,我们到底需不需要overload这两种operation 呢?
答案就是:一般,我们我们需要手动编写析构函数的类,都需要overload 拷贝函数和赋值运算符。
下面介绍类的赋值运算符
1.C++中对象的内存分配方式
在C++中,对象的实例在编译的时候,就需要为其分配内存大小,因此,系统都是在stack上为其分配内存的。这一点和C#完全不同!千万记住:在C#中,所有类都是reference type,要创建类的实体,必须通过new在heap上为其分配空间,同时返回在stack上指向其地址的reference.
因此,在C++中,只要申明该实例,在程序编译后,就要为其分配相应的内存空间,至于实体内的各个域的值,就由其构造函数决定了。
例如:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class A
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A(int id,char *t_name)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_id=id;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
name=new char[strlen(t_name)+1];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(name,t_name);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
private:
char *name;
int _id;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A a(1,"herengang");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A b;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
在程序编译之后,a和b在stack上都被分配相应的内存大小。只不过对象a的域都被初始化,而b则都为随机值。
其内存分配如下:
2. 缺省情况下的赋值运算符
如果我们执行以下:
b=a;
则其执行的是缺省定义的赋值运算。所谓缺省的赋值运算,是指对象中的所有位于stack中的域,进行相应的复制。但是,如果对象有位于heap上的域的话,其不会为拷贝对象分配heap上的空间,而只是指向相同的heap上的同一个地址。
执行b=a这样的缺省的赋值运算后,其内存分配如下:
因此,对于缺省的赋值运算,如果对象域内没有heap上的空间,其不会产生任何问题。但是,如果对象域内需要申请heap上的空间,那么在析构对象的时候,就会连续两次释放heap上的同一块内存区域,从而导致异常。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
~A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete name;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
3.解决办法--重载(overload)赋值运算符
因此,对于对象的域在heap上分配内存的情况,我们必须重载赋值运算符。当对象间进行拷贝的时候,我们必须让不同对象的成员域指向其不同的heap地址--如果成员域属于heap的话。
因此,重载赋值运算符后的代码如下:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class A
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A(int id,char *
16def
t_name)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_id=id;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
name=new char[strlen(t_name)+1];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(name,t_name);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A& operator =(A& a)
//注意:此处一定要返回对象的引用,否则返回后其值立即消失!
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(name!=NULL)
delete name;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this->_id=a._id;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len=strlen(a.name);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
name=new char[len+1];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(name,a.name);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return *this;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
~A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cout<<"~destructor"<<endl;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete name;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int _id;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char *name;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
int main()
{
A a(1,"herengang");
A b;
b=a;
}
其内存分配如下:
这样,在对象a,b退出相应的作用域,其调用相应的析构函数,然后释放分别属于不同heap空间的内存,程序正常结束。
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.username);
}
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.而拷贝函数不需要如此,因为对象此时还没有分配堆空间。
note1:
不要按值向函数传递对象。如果对象有内部指针指向动态分配的堆内存,丝毫不要考虑把对象按值传递给函数,要按引用传递。并记住:若函数不能改变参数对象的状态和目标对象的状态,则要使用const修饰符
note2:问题:
对于类的成员需要动态申请堆空间的类的对象,大家都知道,我们都最好要overload其赋值函数和拷贝函数。拷贝构造函数是没有任何返回类型的,这点毋庸置疑。 而赋值函数可以返回多种类型,例如以上讲的void,类本身class1,以及类的引用 class &? 问,这几种赋值函数的返回各有什么异同?
答:1 如果赋值函数返回的是void ,我们知道,其唯一一点需要注意的是,其不支持链式赋值运算,即a=b=c这样是不允许的!
2 对于返回的是类对象本身,还是类对象的引用,其有着本质的区别!
第一:如果其返回的是类对象本身。
A operator =(A& a)
{
if(name!=NULL)
delete name;
this->_id=a._id;
int len=strlen(a.name);
name=new char[len+1];
strcpy(name,a.name);
return *this;
}
其过程是这样的:
class1 A("herengnag");
class1 B;
B=A;
看似简单的赋值操作,其所有的过程如下:
1 释放对象原来的堆资源
2 重新申请堆空间
3 拷贝源的值到对象的堆空间的值
4 创建临时对象(调用临时对象拷贝构造函数),将临时对象返回
5. 临时对象结束,调用临时对象析构函数,释放临时对象堆内存
my god,还真复杂!!
但是,在这些步骤里面,如果第4步,我们没有overload 拷贝函数,也就是没有进行深拷贝。那么在进行第5步释放临时对象的heap 空间时,将释放掉的是和目标对象同一块的heap空间。这样当目标对象B作用域结束调用析构函数时,就会产生错误!!
因此,如果赋值运算符返回的是类对象本身,那么一定要overload 类的拷贝函数(进行深拷贝)!
第二:如果赋值运算符返回的是对象的引用,
A& operator =(A& a)
{
if(name!=NULL)
delete name;
this->_id=a._id;
int len=strlen(a.name);
name=new char[len+1];
strcpy(name,a.name);
return *this;
}
那么其过程如下:
1 释放掉原来对象所占有的堆空间
1.申请一块新的堆内存
2 将源对象的堆内存的值copy给新的堆内存
3 返回源对象的引用
4 结束。
因此,如果赋值运算符返回的是对象引用,那么其不会调用类的拷贝构造函数,这是问题的关键所在!!
完整代码如下:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// virtual.cpp : Defines the entry point for the console application.
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "string.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "stdlib.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "assert.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class complex
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int real;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int virt;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
complex(){real=virt=0;}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
complex(int treal,int tvirt){real=treal;virt=tvirt;}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
complex operator+(const complex &x)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
real+=x.real;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
virt+=x.virt;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return *this;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
complex operator=(const complex &x)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return complex(x.real,x.virt);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class A
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
A(){m_username=NULL;printf("null constructor");}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A(char *username)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
len=strlen(username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_username=new char[len+1];//(char*)malloc(sizeof(len+1));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(m_username,username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("\nUsername is %s\n",m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A(A &a);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A operator=(A &b);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int test(const int &x)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return x;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
virtual ~A()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// if(m_username)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete m_username;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("\nA is destructed\n");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char *m_username;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
A::A(A &a)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len=strlen(a.m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this->m_username=new char[len+2];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(m_username,a.m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcat(m_username,"f");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("\ndeep copy function");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
A A::operator=(A &b)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(m_username)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete m_username;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len=strlen(b.m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this->m_username=new char[len+1];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(m_username,b.m_username);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// printf("copied successfully!");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return *this;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class B:public A
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
B(char *username,char *password):A(username)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int len=strlen(password)+1;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_password=new char[len];//(char *)malloc(sizeof(len));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strcpy(m_password,password);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("username:%s,password:%s\n",m_username,m_password);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
~B()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete m_password;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("B is destructed\n");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected:
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
char *m_password;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// B b("herengang","982135");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// A *a=&b;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// delete a;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A a("haha");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
A b;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
printf("\nbegin to invoke copy function");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
b=a;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// printf("%d",b.test(2));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//complex x(1,3),y(1,4);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//x=(x+y);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//printf("%d,%d",x.real,x.virt);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return 0;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1 重载赋值运算符返回结果为类对象的运行结果
明显, 运算符最后调用了拷贝构造函数
2 重载赋值运算符返回结果为类对象引用的运行结果
很明显,没有调用拷贝构造函数
相关文章推荐
- operator=返回对象和引用的区别
- Effective C++(10) 重载赋值操作符时,返回该对象的引用(retrun *this)
- 返回vector对象与引用的区别
- c++返回引用和返回对象的区别
- 函数返回对象和返回对象的引用的区别
- C++返回对象和引用的区别
- 返回vector对象与引用的区别
- 赋值运算符重载函数 返回引用和返回对象的区别
- 返回对象和返回对象的引用的区别···
- 返回对象和返回对象的引用的区别
- 为什么有时候C++运算符重载要返回引用,附对象生存周期
- Effective C++ 第二版 22)传引用 23)返回对象 24)函数重载vs缺省值
- 类成员函数返回对象和返回引用的区别
- Effective C++(10) 重载赋值操作符时,返回该对象的引用(retrun *this)
- php 函数引用、变量引用、对象引用的区别
- [翻译] Effective C++, 3rd Edition, Item 21: 当你必须返回一个 object(对象)时不要试图返回一个 reference(引用)(下)
- 调用窗体对象、窗体元素时引用name/id时各浏览器区别
- 对象引用与对象的区别
- c++ 函数符号重载 返回类型是否为引用问题 剖析
- 面向对象重写(override)与重载(overload)区别