赋值函数

c++中对对象的操作一般有赋值和拷贝,其中用到的函数有构造函数,拷贝构造函数和赋值函数。

其中拷贝构造函数和赋值函数的区别是是否有新对象的产生。

赋值函数:对已有对象进行赋值。

string& operator=(const string &s);

 1 String& String::operator=(const String &rhs)
 2 {
 3     if (this == &rhs)
 4         return *this;
 5  
 6     delete[] m_data; m_data = NULL;
 7     m_data = new char[strlen(rhs.m_data) + 1];
 8     strcpy(m_data, rhs.m_data);
 9  
10     return *this;
11 
12 ————————————————
13 版权声明:本文为CSDN博主「selfimpr1991」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
14 原文链接:https://blog.csdn.net/wenqian1991/article/details/29178649
View Code

猜你喜欢

转载自www.cnblogs.com/fanhua666/p/11519596.html