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++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...
随机推荐
- MySQL数据库高并发优化配置
在Apache, PHP, mysql的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分.对于Discuz!论坛程序也是如此,MySQL的设置是否合理优化,直接 影响到论坛的速度和承载量! ...
- POJ-2378 Tree Cutting
题目大意:一棵n个节点的树,找出所有的点满足:删除它之后,产生的最大(这里节点数最多即为最大)新树中节点数不超n的一半. 题目分析:两次深搜,过程类似求重心. 代码如下: # include<i ...
- hdu3416 最短路+最大流
题意:有 n 点 m 边,有出发点 A 到达点 B ,只允许走原图中的最短路,但每条边只允许被走一次,问最多能找出多少条边不重复的最短路 一开始做到的时候瞎做了一发最短路,WA了之后也知道显然不对,就 ...
- 如何更新Android SDK和Build Tool
1. 运行命令 android 2. 勾选并安装需要的版本 3. 完成!
- HTML DOM参考手册
HTML DOM是HTML Document Object Model(文档对象模型)的缩写,HTML DOM则是专门适用与HTML/XHTML的文档对象模型.熟悉软件开发的人员可以将HTML DOM ...
- ps命令详解(转)
原文地址:http://apps.hi.baidu.com/share/detail/32573968 有时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程序 ...
- 图片Exif信息
Exif文件格式简述链接:https://www.zhihu.com/question/23727439/answer/25467748 可交换图像文件常被简称为Exif(Exchangeable i ...
- Python的numpy库下的几个小函数的用法
numpy库是Python进行数据分析和矩阵运算的一个非常重要的库,可以说numpy让Python有了matlab的味道 本文主要介绍几个numpy库下的小函数. 1.mat函数 mat函数可以将目标 ...
- Windows动态链接库DLL
1.什么是DLLDLL,即动态链接库,是包含若干个函数的库文件,可供其他程序运行时调用. 2.DLL的优缺点优点:代码重用,可供多个程序同时调用 缺点:易发生版本冲突当新版本的动态链接库不兼容旧版本时 ...
- IIS6下PHP环境的资源未找到(404)问题
故障现象: 无法找到该页, 404错误