C++赋值运算符重载函数(operator=)
由于对c++的重载符号一直不是很理解,此处参阅一些资料给出比较详细的解释,方便读者以及自己查阅。
此处有更详细的解释,但是其中关于 a = b = c执行顺序的解释不正确!
例1#include<iostream> #include<cstring> using namespace std; class MyStr { private: char *name = NULL; int id; public: MyStr() {cout << "defaultn" << endl;} MyStr(int _id, char *_name) //constructor { cout << "constructor" << endl; id = _id; name = new char[strlen(_name) + 1]; memcpy(name, _name, strlen(_name) + 1); } MyStr(const MyStr& str) { cout << "copy constructor" << endl; id = str.id; if (name != NULL) delete name; name = new char[strlen(str.name) + 1]; memcpy(name, str.name, strlen(str.name) + 1); } MyStr& operator =(const MyStr& str)//赋值运算符 { cout << "operator =" << endl; if (this != &str) { if (name != NULL) delete name; this->id = str.id; int len = strlen(str.name); name = new char[len + 1]; memcpy(name, str.name, strlen(str.name) + 1); } return *this; } ~MyStr() { delete name; } }; int main() { MyStr str1(1, "hhxx"); cout << "====================" << endl; MyStr str2; str2 = str1; cout << "====================" << endl; MyStr str3 = str2; return 0; }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 结果1constructor ==================== default operator = ==================== copy constructor 12345678
此处可以看到执行
str2 = str11
时,调用了赋值运算符重载函数,可以与普通的函数调用一样理解,str2为函数调用者,str1为函数调用的参数(类似于str2.xxx(str1))。
赋值运算符重载函数形参为引用,避免了一次对象的拷贝构造。
同样,复制运算符重载函数函数返回值为引用,同样避免了一次对象的拷贝构造。若返回值类型为MyStr,那么在函数返回时,将构造一个linshi变量,并this指针指向的对象拷贝至该临时变量。
当然,也可以不返回任何对象,设置返回值为void。但是,返回对象或者引用时可以实现连续赋值,类似于a = b = c,等价于a = (b = c);
#include<iostream> #include<cstring> using namespace std; class MyStr { private: char *name = NULL; int id; public: MyStr() {cout << "defaultn" << endl;} MyStr(int _id, char *_name) //constructor { cout << "constructor" << endl; id = _id; name = new char[strlen(_name) + 1]; memcpy(name, _name, strlen(_name) + 1); } MyStr(const MyStr& str) { cout << "copy constructor" << endl; id = str.id; if (name != NULL) delete name; name = new char[strlen(str.name) + 1]; memcpy(name, str.name, strlen(str.name) + 1); } MyStr operator =(const MyStr& str)//赋值运算符 { cout << "operator =" << endl; if (this != &str) { if (name != NULL) delete name; this->id = str.id; int len = strlen(str.name); name = new char[len + 1]; memcpy(name, str.name, strlen(str.name) + 1); } return *this; } ~MyStr() { delete name; } }; int main() { MyStr str1(1, "hhxx"); cout << "====================" << endl; MyStr str2; str2 = str1; cout << "====================" << endl; MyStr str3 = str2; return 0; }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 结果2constructor ==================== default operator = copy constructor ==================== copy constructor 123456789
执行
str2 = str11
时,函数返回的是对象,调用拷贝构造函数产生一个临时变量。
例3(验证 a = b = c执行顺序)#include<iostream> #include<cstring> using namespace std; class MyStr { private: char *name = NULL; int id; public: MyStr() {cout << "defaultn" << endl;} MyStr(int _id, char *_name) //constructor { cout << "constructor" << endl; id = _id; name = new char[strlen(_name) + 1]; memcpy(name, _name, strlen(_name) + 1); } MyStr(const MyStr& str) { cout << "copy constructor" << endl; id = str.id; if (name != NULL) delete name; name = new char[strlen(str.name) + 1]; memcpy(name, str.name, strlen(str.name) + 1); } MyStr& operator =(const MyStr& str)//赋值运算符 { if(name != NULL) cout << name << " = " << str.name << endl; cout << "operator =" << endl; if (this != &str) { if (name != NULL) delete name; this->id = str.id; int len = strlen(str.name); name = new char[len + 1]; memcpy(name, str.name, strlen(str.name) + 1); } return *this; } ~MyStr() { delete name; } }; int main() { MyStr str1(1, "str1"); cout << "====================" << endl; MyStr str2(2, "str2"), str3(3, "str3"); str3 = str2 = str1; cout << "====================" << endl; MyStr str4 = str2; return 0; }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 结果3constructor ==================== constructor constructor str2 = str1 operator = str3 = str1 operator = ==================== copy constructor 1234567891011
可以看到,首先构造是三个对象,name分别为str1, str2, str3。然后执行
str3 = str2 = str11
时,先是执行str2 = str1,对象str2获取str1的name之后,str3将获取str2新获取的name。
相关知识
+=的特殊效果,常量的赋值和变量的赋值,什么是javabean
C++程序设计(上)练习
a=b++,c++和a=(b++,c++)的区别
C/C++ 学习手札(四)
==的作用
为什么 a = (a+b)
C++课程作业之 宠物类的创建
C++中*=是什么意思
1024. 分析四则运算表达式
关于微信小程序常见的运算符
网址: C++赋值运算符重载函数(operator=) https://www.mcbbbk.com/newsview522647.html
上一篇: 递归方程T(n)=aT(n/b) |
下一篇: 陷阱题:输入两个正整数 a 和 |
推荐分享

- 1我的狗老公李淑敏33——如何 5096
- 2南京宠物粮食薄荷饼宠物食品包 4363
- 3家养水獭多少钱一只正常 3825
- 4豆柴犬为什么不建议养?可爱的 3668
- 5自制狗狗辅食:棉花面纱犬的美 3615
- 6狗交配为什么会锁住?从狗狗生 3601
- 7广州哪里卖宠物猫狗的选择性多 3535
- 8湖南隆飞尔动物药业有限公司宠 3477
- 9黄金蟒的价格 3396
- 10益和 MATCHWELL 狗 3352