C++ 类的赋值运算时,构造函数的调用

在C++中重载 "+" 和 "=" 运算符时,构造函数如何调用。

cpp 复制代码
#include <iostream>
using namespace std;

class Box
{
    int length;
    std::string name = "box_default";
public:
     Box(std::string s) {
         name = s;
         std::cout << "Default constructor: "<< name << std::endl;
    }
     Box(const Box& b) {
         std::cout << "Copy constructor: "<< b.name << ", length: " << b.length << std::endl;
         std::cout << "Copy constructor : this : " << this->name <<  std::endl;
         this->length = b.length;
         this->name = "box_copy";
         std::cout << "Copy constructor end: this : " << this->name << ", b : " << b.name << std::endl;
     }
     
    int getLength(void)
    {
        return length;
    }
    void setLength(int len)
    {
        length = len;
    }
    // 重载 + 运算符,用于把两个 Box 对象相加
    Box operator+(const Box& b)
    {
        Box box("box+");
        box.length = this->length + b.length;
        std::cout << "operator+: this: " << this->name << " ,b: " << b.name << std::endl;
        return box;
    }

    Box& operator = (const Box& b) {
        std::cout << "operator=: this: " << this->name << ", b: " << b.name << std::endl;
        std::cout << "length: " << b.length << std::endl;
        this->name = b.name;
        this->length = b.length;
        return *this;
    }
};
// 程序的主函数
int main()
{
    Box Box1("box1");                // 声明 Box1,类型为 Box
    Box Box2("box2");                // 声明 Box2,类型为 Box
    Box Box3("box3");                // 声明 Box3,类型为 Box

    // Box1 详述
    Box1.setLength(6);

    // Box2 详述
    Box2.setLength(12);

    // Box1 
    cout << "Length of Box1 : " << Box1.getLength() << endl;

    // Box2 
    cout << "Length of Box2 : " << Box2.getLength() << endl;

    // 把两个对象相加,得到 Box3
    Box3 = Box1 + Box2;

    // Box3 
    cout << "Length of Box3 : " << Box3.getLength() << endl;

    return 0;
}

运行上述代码后的结果:

cpp 复制代码
Default constructor: box1
Default constructor: box2
Default constructor: box3
Length of Box1 : 6
Length of Box2 : 12
Default constructor: box+
operator+: this: box1 ,b: box2
Copy constructor: box+, length: 18
Copy constructor : this : box_default
Copy constructor end: this : box_copy, b : box+
operator=: this: box3, b: box_copy
length: 18
Length of Box3 : 18

由上述结果可知:

在执行 Box3 = Box1 + Box2; 这句代码时, 先调用了 operator+, 随后调用拷贝构造函数 Box(const Box& b), 最后调用 operator =.

  1. 调用 operator+: 先创建一个 Box 对象 box+, this 指针隐式指向 Box1, Box2作为 引用参数
  2. 调用拷贝构造函数 Box(const Box& b): 此时会调用拷贝构造函数拷贝 operator+ 的返回值的副本作为 operator = 运算的右值
  3. 最后调用 operator =.
相关推荐
MediaTea5 分钟前
Python 库手册:doctest 文档测试模块
开发语言·python·log4j
hweiyu0028 分钟前
R语言简介(附电子书资料)
开发语言·r语言
hweiyu0030 分钟前
R语言常用扩展包
开发语言·r语言
拳里剑气1 小时前
C语言:顺序表(上)
c语言·开发语言·数据结构·学习方法
王者鳜錸1 小时前
PYTHON从入门到实践-15数据可视化
开发语言·python·信息可视化
杨航 AI1 小时前
ADB+Python控制(有线/无线) Scrcpy+按键映射(推荐)
开发语言·python·adb
研究司马懿1 小时前
【Golang】Go语言函数
开发语言·后端·golang
郝学胜-神的一滴2 小时前
Python defaultdict 的强大之处:告别繁琐的字典键检查: Effective Python 第17条
开发语言·python·程序人生
码界奇点2 小时前
Java同步锁性能优化:15个高效实践与深度解析
java·开发语言·性能优化·java-ee·同态加密
Sally璐璐2 小时前
Python系统交互库全解析
开发语言·python·php