首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >赋值运算符不被调用

赋值运算符不被调用
EN

Stack Overflow用户
提问于 2019-10-24 06:13:17
回答 1查看 95关注 0票数 3
代码语言:javascript
复制
#include <iostream>

class MemoryBlock {
private:
    int length = 0;
    int* m_arr = nullptr;

public:
    // Constructor
    explicit MemoryBlock(int p_length) : length(p_length), m_arr(new int[length]) {
        std::cout << "In MemoryBlock(int), length is " << length << ".\n";
    }

    // Destructor
    ~MemoryBlock() {
        std::cout << "~MemoryBlock() is called. Deleting resources.\n";

        delete[] m_arr;
    }

    // Copy constructor
    MemoryBlock(const MemoryBlock& other) : length(other.length), m_arr(new int[other.length]) {
        std::cout << "In MemoryBlock(const MemoryBlock&), length is " << length << ".\n";

        for (int i = 0; i < length; ++i) {
            m_arr[i] = other.m_arr[i];
        }
    }

    // Move constructor
    MemoryBlock(MemoryBlock&& other) noexcept : length(0), m_arr(nullptr) {
        std::cout << "In MemoryBlock(MemoryBlock&&), length is " << other.length << ".\n";

        length = other.length;
        m_arr = other.m_arr;

        other.length = 0;
        other.m_arr = nullptr;
    }

    // Addition operator overloading
    MemoryBlock operator+(const MemoryBlock& other) {
        std::cout << "In operator+(const MemoryBlock&), adds length and array space. New length: " << length + other.length << '\n';

        int added_length = length + other.length;
        MemoryBlock sum(added_length);

        int idx = 0;
        int other_idx = 0;

        for (int i = 0; i < added_length; ++i) {
            if (i < length) {
                sum.m_arr[i] = m_arr[idx];
                ++idx;
            } else {
                sum.m_arr[i] = other.m_arr[other_idx];
                ++other_idx;
            }
        }

        return sum;
    }

    // Copy assignment operator
    MemoryBlock& operator=(const MemoryBlock& other) {
        std::cout << "In operator=(const MemoryBlock&), length is " << other.length << ".\n";

        if (this != &other) {
            delete[] m_arr;

            length = other.length;
            m_arr = new int[other.length];

            for (int i = 0; i < length; ++i) {
                m_arr[i] = other.m_arr[i];
            }
        }

        return *this;
    }

    // Move assignment operator
    MemoryBlock& operator=(MemoryBlock&& other) noexcept {
        std::cout << "In operator=(MemoryBlock&&), length is " << other.length << ".\n";

        if (this != &other) {
            delete[] m_arr;

            length = other.length;
            m_arr = other.m_arr;

            other.length = 0;
            other.m_arr = nullptr;
        }

        return *this;
    }
};

int main() {
    MemoryBlock mb1(11);
    MemoryBlock mb2(mb1);
    MemoryBlock mb3 = mb1 + mb2;

    std::cout << "----------------- End of main() -----------------\n";

    return 0;
}

运行代码

我从微软开发人员文档获取了这个示例代码。我已经生成了对象mb1并将其复制到mb2中。然后将mb1 + mb2分配给mb3。但是正如您在结果中所看到的,赋值运算符不被调用。

此外,似乎临时对象是在调用加法运算符之后生成的,但它的析构函数不被调用。被调用的析构函数是mb1、mb2和mb3的析构函数。

为什么即使有一个句子MemoryBlock mb3 = mb1 + mb2;也不调用赋值运算符?为什么临时对象的析构函数没有调用?

结果:

代码语言:javascript
复制
In MemoryBlock(int), length is 11.
In MemoryBlock(const MemoryBlock&), length is 11.
In operator+(const MemoryBlock&), adds length and array space. New length: 22
In MemoryBlock(int), length is 22.  // Maybe temporary object?
----------------- End of main() -----------------
~MemoryBlock() is called. Deleting resources.
~MemoryBlock() is called. Deleting resources.
~MemoryBlock() is called. Deleting resources.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-24 06:16:44

MemoryBlock mb3 = mb1 + mb2;调用复制构造函数。

MemoryBlock mb3; mb3 = mb1 + mb2;调用赋值运算符。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58535165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档