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

#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. 通过UserAgent判断智能手机(设备,Android,IOS)

    转:http://free0007.iteye.com/blog/2017329 /// 根据 Agent 判断是否是智能手机 ///</summary> ///<returns&g ...

  2. socket模块

    1 1.1 server: #!/use/local/env python# -*- coding:utf-8 -*- import socket ip_port = ('127.0.0.1', 99 ...

  3. GDB动态库搜索路径

    当GDB无法显示so动态库的信息或者显示信息有误时,通常是由于库搜索路径错误导致的,可使用set sysroot.set solib-absolute-prefix.set solib-search- ...

  4. linq实现左连接

    1.左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals ...

  5. Java C# 加密解密类库

    Bouncy Castle 是一种用于 Java 平台的开放源码的轻量级密码术包.它支持大量的密码术算法,并提供 JCE 1.2.1 的实现.因为 Bouncy Castle 被设计成轻量级的,所以从 ...

  6. Innodb IO优化 — 数据库表设计 转

    数据库表设计这块学问比较多,我这里单从互联网角度出发同时结合Innodb的特性给出一些设计方法供大家参考.本文构建大概分两分部分:Innodb的特性及设计中如何利用这种特性. Innodb特性: In ...

  7. EditText的圆角与边框

    先看一下效果图: 先创建一个xml文件edittext_shape.xml: <?xml version="1.0" encoding="utf-8"?& ...

  8. DataRow[] 转为数组

    DataRow[] rows = dt.Select("1=1"); ].ToString()).ToArray();

  9. Redis Cluster 在PHP上的实践

    版本:redis3.0.3 redis——slot: 'master' => [ '192.168.1.55:7000'=>[0,5460], '192.168.1.55:7001'=&g ...

  10. jsp常用指令

    Jsp包含三个编译指令和七个动作指令. 三个编译指令为:page.include.taglib. 七个动作指令为:jsp:forward.jsp:param.jsp:include.jsp:plugi ...