重载=号运算符,由于成员属性中有指针会出现错误

#include <iostream>
using namespace std; class num{
public:
num(){n=new int;*n=;cout<<"construct:"<<endl;} num(int x){n=new int;*n=x;cout<<"construct:"<<endl;} ~num(){delete n;n=NULL; cout<<"destruct:"<<endl;} //num(num &a){this->x=a.x;cout<<"copy:"<<x<<endl;} int getX(){
return *n;
} void setX(int x)
{
*n=x;
} num operator=(num &r){
cout<<"operator+"<<endl;
*n=r.getX();
return *this; //返回two的副本,two的副本返回后进行析构,导致n指向的内存释放
} private:
int *n; }; int & test(int & x)
{
cout<<x<<endl;
return x;
} int main()
{ num one,two,three;
one.setX();
two=one; //<==> two.operator =(one); cout<<two.getX()<<endl; return ; }

解决上面的错误:(深拷贝)

#include <iostream>
using namespace std; class num{
public:
num(){n=new int;*n=;cout<<"construct:"<<endl;} num(int x){n=new int;*n=x;cout<<"construct:"<<endl;} ~num(){delete n;n=NULL; cout<<"destruct:"<<endl;} num(const num & a){n=new int;*n=a.getX();cout<<"copy:"<<endl;} int getX() const {
return *n;
} void setX(int x)
{
*n=x;
} num operator=(num &r){
cout<<"operator+"<<endl;
*n=r.getX();
return *this; //返回two的副本,two的副本返回后进行析构,导致n指向的内存释放
} private:
int *n; }; int main()
{ num one,two,three;
one.setX();
three=two=one; //<==> two.operator =(one); cout<<one.getX()<<endl;
cout<<two.getX()<<endl;
cout<<three.getX()<<endl; return ; }

解决上面的错误(引用方式返回)、

#include <iostream>
using namespace std; class num{
public:
num(){n=new int;*n=;cout<<"construct:"<<endl;} num(int x){n=new int;*n=x;cout<<"construct:"<<endl;} ~num(){delete n;n=NULL; cout<<"destruct:"<<endl;} num(const num & a){n=new int;*n=a.getX();cout<<"copy:"<<endl;} int getX() const {
return *n;
} void setX(int x)
{
*n=x;
} const num & operator=(const num &r){
cout<<"operator+"<<endl; if(this == &r)
{
return *this;
} *n=r.getX();
return *this; //返回two的副本,two的副本返回后进行析构,导致n指向的内存释放
} private:
int *n; }; class man{
public :
man(int x){a=x;}
man(){}
public :
int a;
}; int main()
{
num one(),two,three;
three=two=one; cout<<one.getX()<<endl;
cout<<two.getX()<<endl;
cout<<three.getX()<<endl; return ; }
#include <iostream>
using namespace std; class num{
public:
num(){n=new int;*n=;cout<<"construct:"<<endl;} num(int x){n=new int;*n=x;cout<<"construct:"<<endl;} ~num(){delete n;n=NULL; cout<<"destruct:"<<endl;} num(const num & a){n=new int;*n=a.getX();cout<<"copy:"<<endl;} int getX() const {
return *n;
} void setX(int x)
{
*n=x;
} // const num & operator=(const num &r){
// cout<<"operator+"<<endl; // if(this == &r)
// {
// return *this;
// } // *n=r.getX();
// return *this; //返回two的副本,two的副本返回后进行析构,导致n指向的内存释放
//} private:
int *n; }; class man{
public :
man(int x){a=x;}
man(){}
public :
int a;
}; int main()
{
num one(),two,three;
two=one; /**
错误原因:当执行 two=one; 后,两个对象的成员属性,执行了同一内存地址,其中一个先析构了
遍释放了a执行的内存地址,另个对象在析构时便会报错了 */ cout<<one.getX()<<endl;
cout<<two.getX()<<endl; return ; }

类型转换

#include <iostream>
using namespace std; class A{
public:
A(int x, int y=){i=x; cout<<"construct"<<i<<endl;}
~A(){cout<<"destruct"<<i<<endl;}
void geti(){cout<<i<<endl;}
private:
int i;
}; int main()
{
A a();
a=; //相当于 a= A(20); ,先创建一个临时的对象,然后将这个临时对象赋给对象a,完成赋值后调用临时对象的析构函数 //或 a=A(20); return ; }

c++学习-运算符重载的更多相关文章

  1. Python 中的运算符重载

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 一种运算符对于不同类型的对象,有不同的使用方式.例如, + 用于整型对象,表示两个数相加:用于字符串 ...

  2. C++学习笔记之运算符重载

    一.运算符重载基本知识 在前面的一篇博文 C++学习笔记之模板(1)——从函数重载到函数模板 中,介绍了函数重载的概念,定义及用法,函数重载(也被称之为函数多态)就是使用户能够定义多个名称相同但特征标 ...

  3. C++学习之运算符重载的总结

    C++学习之运算符重载的总结              运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用域不同类型的数据导致不同行为的发生,C++为运算符重载提供了一种方法,即运算符重载函数 ...

  4. 初步C++运算符重载学习笔记&lt;3&gt; 增量递减运算符重载

    初步C++运算符重载学习笔记<1> 初探C++运算符重载学习笔记<2> 重载为友元函数     增量.减量运算符++(--)分别有两种形式:前自增++i(自减--i).后自增i ...

  5. 初探C++运算符重载学习笔记&lt;2&gt; 重载为友元函数

    初探C++运算符重载学习笔记 在上面那篇博客中,写了将运算符重载为普通函数或类的成员函数这两种情况. 以下的两种情况发生.则我们须要将运算符重载为类的友元函数 <1>成员函数不能满足要求 ...

  6. C++运算符重载学习总结

    在C ++中,我们可以使运算符适用于用户定义的类. 这意味着C ++能够为运算符提供数据类型的特殊含义,这种能力称为运算符重载. 例如,我们可以在像String这样的类中重载运算符'+',这样我们就可 ...

  7. c++中的运算符重载operator2(翁恺c++公开课[31-33]学习笔记)

    上一篇operator1中,大概说了下重载的基本用法,接下来对c++中常见的可重载运算符归一下类,说一下它们的返回值,讨论下较为复杂的运算符重载上的坑

  8. c++中的运算符重载operator1(翁恺c++公开课[30]学习笔记)

    运算符重载规则: 只有已经存在的运算符才能被重载,不能自己制造一个c++中没有的运算符进行重载 重载可以在类或枚举类型内进行,也可以是全局函数,但int.float这种已有的类型内是不被允许的 不能二 ...

  9. C++基础 学习笔记五:重载之运算符重载

    C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...

随机推荐

  1. MySQL中日期与字符串相互转换,并进行日期比较查询

    技术交流群:233513714 1.日期无需转换查询(日期在数据库中的类型为字符串) select * from day where dateTime > '2016-03-15' 2.使用da ...

  2. 论文笔记之:Learning to Track: Online Multi-Object Tracking by Decision Making

    Learning to Track: Online Multi-Object Tracking by Decision Making ICCV   2015 本文主要是研究多目标跟踪,而 online ...

  3. MMU讲解

    MMU是Memory Management Unit的缩写,中文名是内存管理单元,它是中央处理器(CPU)中用来管理虚拟存储器.物理存储器的控制线路,同时也负责虚拟地址映射为物理地址,以及提供硬件机制 ...

  4. 胶囊碰撞体(CapsuleCollider)

    胶囊碰撞体 (Capsule Collider) 胶囊碰撞体 (Capsule Collider) 由两个半球体与一个圆柱体相连接而构成.它与胶囊 (Capsule) 基元形状相同.   一堆胶囊碰撞 ...

  5. easyUI之Combo

    Combo组件为自定义下拉列表组件,无class的加载方式,主要是通过jquery的方式.它依赖于validatebox,可以用它的很多属性.例如: 前台: <div id="box& ...

  6. 一次zabbix的渗透

    wget http://xxxxxxx:8888/back.py -O  /tmp/1.py  写入python反弹马 反弹到vps python /tmp/back.py IP port       ...

  7. OpenJudge计算概论-求满足条件的3位数

    /*======================================================================== 求满足条件的3位数 总时间限制: 1000ms 内 ...

  8. OpenJudge计算概论-发票统计

    /*====================================================================== 发票统计 总时间限制: 1000ms 内存限制: 65 ...

  9. 无法在web服务器上启动调试。Microsoft Visual Studio 远程调试监视器(MSVSMON.EXE)似乎没有在远程计算机上运行,VS2012调试错误

    1.重启(无用) 2.关闭防火墙(无用) 3.开启文件与打印机共享(无用) 4.无远程调试权限,改为本地调试.或者是IIS中此项目没有启动.或者没有在IIS中新建此项目.

  10. replace() replace_copy()

    int a[] = {1,2,3,3,4}; vector<int> v(a, a+5); vector<int> v2; //replace(v.begin(), v.end ...