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++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...
随机推荐
- hdu2647 拓扑序
题意:年终要给 n 个员工发奖金,每个人的起始金额是888,有些人觉得自己做的比另一个人好所以应该多得一些钱,问最少需要花多少钱,如果不能满足所有员工的要求,输出 -1 拓扑排序,从奖金少的向奖金多的 ...
- network Driver , TDI(Transport Driver Interface) Drivers
https://msdn.microsoft.com/en-us/library/windows/hardware/ff565094(v=vs.85).aspx https://msdn.micros ...
- Unity3d NGUI的使用(九)(UIScrollView制作滑动列表)
UIScrollView制作滑动列表,可横向,竖直展示一些列表在固定可视范围内 UIScrollVIew只是一个可滑动的UI组件 如果需要制作复杂的可视区域UI需要配合使用UIPanel与UIGrid ...
- 解决方法:loadrunner 场景下执行webservice脚本是---报错10492 Error: Exception was raised when calling per-process-init function in extens
在vug下执行时,脚本无异常,但是在controller下执行时报下面错误,网上查了下,解决方法千奇百怪,但无一可行. 分析了下错误,似乎是初始化进程有关.想到rts中的设置习惯时以线程方式执行. 遂 ...
- Druid是什么和用StatViewServlet用于展示Druid的统计信息
Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource 高效可管理的数据库连接池 ...
- Oracle数据库—— PL/SQL进阶编程
一.涉及内容 1.掌握PL/SQL程序块的结构 2.理解并熟练掌握各种变量的应用. 二.具体操作 1.创建一个表messages,该表只有一个字段results 类型是number(2),编写一个块, ...
- js中冒泡事件和捕获事件
js中冒泡事件和捕获事件: 冒泡事件:冒泡事件是从里向外,即是从被绑定元素开始一直向外到达页面的所有祖先元素都会被触发,这 一过程被称为事件冒泡.这个事件从原始元素开始一直冒泡到DOM树的最上层 捕获 ...
- 搭建EF6.0+MVC4搭建框架遇到的问题及解决方案
问题一:“未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” ...
- git基本使用
说明:当前在自己的分支:self:远端开发分支:dev. 1. 基本命令: git status git add . git commit -m 'modify' //从远端dev拉取 git pul ...
- Pop Sequence
题目来源:PTA02-线性结构3 Pop Sequence (25分) Question:Given a stack which can keep M numbers at most. Push ...