C++中赋值运算符重载.doc_第1页
C++中赋值运算符重载.doc_第2页
C++中赋值运算符重载.doc_第3页
C++中赋值运算符重载.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

赋值运算符重载虽然一个对象可以由一个单一的赋值语句赋值给另一个对象,我们之前提到的,此操作可能只创建一个逻辑复制(即,由一个成员复印件)。在一个逻辑复制,在一个对象的成员将该成员的值在其他对象。如果一个类有一个指针成员,由成员复印件使指针成员在两个对象指向同一个对象。有两个问题与此成员对成员的复制:第一,因为指针在两个对象共享相同的变量,这个变量的变化而变化的对象。在其他的话,这两个对象都不是独立的。第二,如果共享变量在堆上,我们不知道谁是负责释放内存。这记忆是不自由的,也将是免费的两倍,这是不允许。当一个类成员是一个指向堆内存,我们通常需要过载赋值操作符来创建一个物理副本。实例1:/说明动态内存分配的默认分配的危险。#include #include const int MAX_CHAR = 10;class Accountfriend ostream& operator strlen(title) )char* tmp = title;title = new charstrlen(newname)+1;strcpy (title,newname);delete tmp;elsestrcpy (title,newname);void changename(char* newname)strcpy (owner,newname);Account () delete title;private:char * title;char ownerMAX_CHAR;float balance;ostream& operator (ostream& os, Account &b)os who: b.title b.owner how much=b.balancen;os endl;return os;void main()Account acc(Lou,Mr, 100);Account acc1;cout acc;cout acc1;acc1=acc;cout acc1;acc1.changename(jean);cout acc1;coutacc;acc1.changetitle(Dr.);cout acc1;cout实例2:/这个例子定义了类的向量的第二版,具有过载运算符=,/和。#include class Vectorpublic:Vector(int s, int an_array ); / a constructor Vector( )delete rep; / a destructorint get_size( ) const return size; / an accessorconst Vector& operator=(const Vector& x);int& operator (int index) return repindex;llconst int& operator (int index) const return repindex;private:int *rep;int size;/ A constructor initializing the members of rep by theparameter an_arrayVector: Vector(int s, int an_array ):size(s), rep(newints)for (int i = 0; i size; +i)repi = an_arrayi;/ Note that the initializer uses new ints to initializerep, but not new intsize/ because the initializers may not be evaluated in thespecified order.const Vector& Vector:operator=(const Vector& x)if( this != &x)size = x.size;delete rep; / clean up the old one.rep = new intsize;for (int i = 0; i size; +i)repi = x.repi;/*size = x.size;if (rep != x.rep)delete rep; / clean up the old one.rep = new intsize;for (int i = 0; i size; +i)repi = x.repi;*/return *this;ostream& operator(ostream& out, const Vector& x)int s = x.get_size( );for (int i = 0; i s; +i)out xiendl;return out;请注意,在这个类中,矢量是通过一个动态数组表示。赋值操作符,复制构造函数和析构函数是必要的。在类向量以前的版本,我们不需要这些功能,因为一个物理副本将由编译器创建的如果分配或复制发生。操作员+ =。+,* =,和/ =也以同样的方式超载。我们也可以使用这些运算符过载算子。例如,我们可以实现运算符+=在复合类如下:const Complex& Complex:operator+= (const Complex& c)r += c._r;i += c._i;return *this;Then, the operator + can be overloaded as the following:const Complex operaotr+ (const Complex& c1, const Complex&c2)Complex temp(c1);temp += c2;return temp;Or, implement

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论