简体   繁体   English

未调用赋值运算符

[英]Assignment operator is not called

#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;
}

Run code运行代码

I took this sample code from Microsoft developer docs .我从Microsoft 开发人员文档中获取了这个示例代码。 I've generated the object mb1 and copied it to mb2.我已经生成了 object mb1 并将其复制到 mb2。 Then assigned mb1 + mb2 to mb3.然后将mb1 + mb2分配给 mb3。 But as you can see in the result, assignment operator is not called.但正如您在结果中看到的那样,没有调用赋值运算符。

Also, it seems the temporary object is generated after addition operator is called, but its destructor is not called.此外,似乎在调用加法运算符后生成了临时 object,但没有调用其析构函数。 The called destructor is mb1, mb2, and mb3's one.调用的析构函数是 mb1、mb2 和 mb3 的一个。

Why assignment operator is not called even though there is a sentence MemoryBlock mb3 = mb1 + mb2;为什么即使有一句话MemoryBlock mb3 = mb1 + mb2;也不调用赋值运算符? ? And why temporary object's destructor hasn't called?为什么没有调用临时对象的析构函数?

RESULT:结果:

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.

MemoryBlock mb3 = mb1 + mb2; calls the copy constructor.调用复制构造函数。

MemoryBlock mb3; mb3 = mb1 + mb2; calls the assignment operator.调用赋值运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM