c++学习-运算符重载
重载=号运算符,由于成员属性中有指针会出现错误
#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++学习-运算符重载的更多相关文章
- Python 中的运算符重载
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 一种运算符对于不同类型的对象,有不同的使用方式.例如, + 用于整型对象,表示两个数相加:用于字符串 ...
- C++学习笔记之运算符重载
一.运算符重载基本知识 在前面的一篇博文 C++学习笔记之模板(1)——从函数重载到函数模板 中,介绍了函数重载的概念,定义及用法,函数重载(也被称之为函数多态)就是使用户能够定义多个名称相同但特征标 ...
- C++学习之运算符重载的总结
C++学习之运算符重载的总结 运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用域不同类型的数据导致不同行为的发生,C++为运算符重载提供了一种方法,即运算符重载函数 ...
- 初步C++运算符重载学习笔记<3> 增量递减运算符重载
初步C++运算符重载学习笔记<1> 初探C++运算符重载学习笔记<2> 重载为友元函数 增量.减量运算符++(--)分别有两种形式:前自增++i(自减--i).后自增i ...
- 初探C++运算符重载学习笔记<2> 重载为友元函数
初探C++运算符重载学习笔记 在上面那篇博客中,写了将运算符重载为普通函数或类的成员函数这两种情况. 以下的两种情况发生.则我们须要将运算符重载为类的友元函数 <1>成员函数不能满足要求 ...
- C++运算符重载学习总结
在C ++中,我们可以使运算符适用于用户定义的类. 这意味着C ++能够为运算符提供数据类型的特殊含义,这种能力称为运算符重载. 例如,我们可以在像String这样的类中重载运算符'+',这样我们就可 ...
- c++中的运算符重载operator2(翁恺c++公开课[31-33]学习笔记)
上一篇operator1中,大概说了下重载的基本用法,接下来对c++中常见的可重载运算符归一下类,说一下它们的返回值,讨论下较为复杂的运算符重载上的坑
- c++中的运算符重载operator1(翁恺c++公开课[30]学习笔记)
运算符重载规则: 只有已经存在的运算符才能被重载,不能自己制造一个c++中没有的运算符进行重载 重载可以在类或枚举类型内进行,也可以是全局函数,但int.float这种已有的类型内是不被允许的 不能二 ...
- C++基础 学习笔记五:重载之运算符重载
C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...
随机推荐
- ZOJ-3933 Team Formation (二分图最佳完美匹配)
题目大意:n个人,分为两个阵营.现在要组成由若干支队伍,每支队伍由两个人组成并且这两个人必须来自不同的阵营.同时,每个人都有m个厌恶的对象,并且厌恶是相互的.相互厌恶的人不能组成一支队伍.问最多能组成 ...
- <C Traps and Pitfalls>笔记
//------------------------------------------------------------------------------ 2.1 理解函数的声明: 编写一个独立 ...
- java多线程之:SynchronousQueue队列
SynchronousQueue是这样一种阻塞队列,其中每个 put 必须等待一个 take,反之亦然.同步队列没有任何内部容量,甚至连一个队列的容量都没有. 不能在同步队列上进行 peek ...
- eclipse adt 项目依赖,使用git上的项目
从git下载的android工程不能直接导入eclipse,有的项目要依赖这个项目,怎么办呢 好像只能新建一个android项目,然后把libs,res,src,AndroidManifest.xml ...
- Page cache和Buffer cache[转1]
http://www.cnblogs.com/mydomain/archive/2013/02/24/2924707.html Page cache实际上是针对文件系统的,是文件的缓存,在文件层面上的 ...
- CSS控制print打印样式
来源:http://blog.csdn.net/pangni/article/details/6224533 一.添加打印样式 1. 为屏幕显示和打印分别准备一个css文件,如下所示: 用于屏幕显 ...
- linux下文件合并、分割、去重
1.文件合并 1.1文件上下合并 cat f1 f2> muti (将文件f1.f2合并成文件muti,f1在上,f2在下) 1.2左右合并 paste f1 f2 > muti (将 ...
- 【转】第7篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:全自动注册与反射方法分析
作者: 牛A与牛C之间 时间: 2013-12-12 分类: 技术文章 | 2条评论 | 编辑文章 主页 » 技术文章 » 第7篇:Xilium CefGlue 关于 CLR Object 与 JS ...
- python中时间日期格式化符号
python中时间日期格式化符号: import time print(time.strftime('%Y%H%M%S', time.localtime())) 运行结果: 2016092308 %y ...
- redis 服务访问密码设定
1. 更改redis.conf配置 # requirepass foobared 去掉注释,foobared改为 自己的password , 我测试的时候用的是 redis-password 2.启动 ...