重载赋值运算符 && 对象
class CMessage
{
private:
char * m_pMessage; public:
void showIt()const
{
cout << m_pMessage << endl;
}
//构造函数
CMessage(const char* text="Default message")
{
cout << "Constructor difinition" << endl; size_t length{strlen(text)+};
m_pMessage = new char[length+];
strcpy_s(m_pMessage,length+,text);
}
//复制构造函数
CMessage(const CMessage & aMess)
{
size_t len{strlen(aMess.m_pMessage)+};
this->m_pMessage = new char[len];
strcpy_s(m_pMessage,len,aMess.m_pMessage);
}
//重载赋值运算符
CMessage & operator=(const CMessage & aMess)
{
if (this!= &aMess)
{
delete[]m_pMessage;
size_t length{strlen(aMess.m_pMessage)+};
m_pMessage = new char[length];
strcpy_s(this->m_pMessage,length,aMess.m_pMessage);
}
return *this;
} //析构函数
~CMessage()
{
cout << "Destructor called" << endl;
delete[]m_pMessage;
}
}; int main()
{
CMessage motto1{"Amiss is as good as a mile"};
CMessage motto2; motto2 = motto1; motto2.showIt();
motto1.showIt(); return ;
}
复制构造函数:
当某个类动态的为数据成员分配空间,又利用按值传递给函数传递该类的对象,那么必须要实现复制构造函数
如:
motto2 {motto1};
或
CMessage & displayMessage(CMessage localMsg)
{
cout <<"the message is:----------------" << endl;
localMsg.showIt();
return *this;
}
都会出现异常,如果没有实现复制构造函数,即两个对象的指针指向了同一块地址区域。
重载赋值运算符:
以现有的同类对象进行初始化类的对象,或者通过按值传递方式给函数传递对象,调用默认复制构造函数。
当赋值语句的左边和右边是同类类型的对象时,调用默认赋值运算符。
如:
motto2 = motto1;
如果我们没有实现赋值运算符函数,编译器就会使用默认的赋值运算符函数,当为数据成员动态的分配内存,进行对象赋值,就会程序异常。
因为两个对象都有一个指向相同内存地址的指针。
分析赋值运算符函数:
CMessage & operator=(const CMessage & aMess)
{ if (this!= &aMess)
{
delete[]m_pMessage;
size_t length{strlen(aMess.m_pMessage)+1};
m_pMessage = new char[length];
strcpy_s(this->m_pMessage,length,aMess.m_pMessage);
}
return *this;
}
delete[]m_pMessage; 删除分配给第一个对象的内存,重新分配足够的内存,以容纳第二个对象的字符串。
1、当 motto1 = motto1;会发生什么呢?
赋值运算符函数会释放 motto1 对象成员指向的内存
所以,if (this!= &aMess) 这条语句,是有必要的。
2、那么返回对象为什么?
motto1 = motto2=motto3;
这条语句的原型是:
motto1.operator=(motto2.operator=(motto3));
可知 motto2.operator=(motto3) 的结果必须是对象才能作为 motto1.operator=()的参数。
3、那么返回引用为什么?
(motto1 = motto2)=motto3;
这条语句的原型是:
(motto1.operator=(motto2)).operator=(motto3);
可知(motto1.operator=(motto2))的结果是个是个返回的临时对象,它是 rvalue ,编译器不允许使用 rvalue 调用函数成员。
而返回引用它是 lvalue。
重载赋值运算符 && 对象的更多相关文章
- 《挑战30天C++入门极限》C++运算符重载赋值运算符
C++运算符重载赋值运算符 自定义类的赋值运算符重载函数的作用与内置赋值运算符的作用类似,但是要要注意的是,它与拷贝构造函数与析构函数一样,要注意深拷贝浅拷贝的问题,在没有深拷贝浅拷贝的情况下 ...
- mfc 重载赋值运算符
重载赋值运算符= 一.重载运算符格式 返回类型 operator 运算符 (参数); 如: bool operator=(char*s); int operator>(char*s); bool ...
- 《剑指offer》第一题(重载赋值运算符)
//重载赋值运算符 #include <iostream> #include <cstring> using namespace std; class CMystring { ...
- Python学习手册之内部方法、操作符重载和对象生命周期
在上一篇文章中,我们介绍了 Python 的类和继承,现在我们介绍 Python 的内部方法.操作符重载和对象生命周期. 查看上一篇文章请点击:https://www.cnblogs.com/dust ...
- C++重载赋值运算符
这是一道C++的面试题,下面在这篇博客中分析一下这个问题.先上题目: //题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: ...
- Java基础语法04面向对象上-类-属性-方法-可变参数-重载-递归-对象数组
类 面向对象是一种思想,一般指将事务的属性与方法抽出总结为模板(类/class),处理事务时通过类创建/new出对象由对象的功能/方法去完成所要计算处理的事情. 面向过程:POP:以过程,步骤为主,考 ...
- JAVA笔记2__类/封闭性/构造方法/方法的重载/匿名对象
public class Main { public static void main(String[] args) { Chicken c1 = new Chicken(); Chicken c2 ...
- C++本质:类的赋值运算符=的重载,以及深拷贝和浅拷贝
关键词:构造函数,浅拷贝,深拷贝,堆栈(stack),堆heap,赋值运算符摘要: 在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作. 如果对象在申明的同时马上进行的初始化操作 ...
- C++学习31 重载=(赋值运算符)
和普通变量一样,对象之间也可以相互赋值.赋值运算符“=”可以用来将一个对象拷贝给另一个已经存在的对象.对象之间的赋值是将成员变量依次拷贝,而不是将整个对象的内存按位拷贝. 对象之间的赋值: #incl ...
随机推荐
- android 监听软键盘的收起与打开
参考: http://toughcoder.net/blog/2015/10/09/android-trick-detect-soft-keyboard-show-slash-hide/ packag ...
- Spring3系列8- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Mac OSX 安装nvm(node.js版本管理器)
我的系统 1.打开github官网https://github.com/,输入nvm搜索,选择creationix/nvm,打开 2.找到Install script,复制 curl -o- http ...
- asp.net MVC之 自定义过滤器(Filter)
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- visual studio snippets风格
snippet挺好用,但是不是我喜欢的那种风格,比如if是这样的 if () { XX } 而我比较习惯这种: if () { XX } 可以这么做: 工具(Tools)——代码段管理器(Code S ...
- 各种UserAgent的列表
User Agent是浏览器用于 HTTP 请求的用户代理头的值.更换User Agent能更好的模拟出不同的系统和浏览器信息. Android Name User Agent Nexus 7 (Ta ...
- Node.js 的module 系统
相较于原生的JavaScript,不同的JavaScript文件之间很难共享变量.有鉴于此,Node.js在JavaScript的基础上进行了扩充,引入了require,exports,module三 ...
- c# 靠谱的bitmap转byte[]
public static byte[] Bitmap2Byte(Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { ...
- Windows Azure Compute Emulator无法启动问题解决方案
Visual Studio 2013安装了Windows Azure SDK 2.7,但启动Emulator调试时,出现如下错误: Error: The installed Windows Azure ...
- rails利用big_sitemap生成sitemap
# Gemfile gem 'big_sitemap' # lib/tasks/sitemap.rake require 'big_sitemap' namespace :custom do desc ...