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 =.
相关推荐
软件黑马王子1 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫1 小时前
go orm GORM
开发语言·后端·golang
黑不溜秋的2 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
李白同学3 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?4 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农4 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿4 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!5 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴5 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程5 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛