gpt4 book ai didi

c++ - 不调用赋值运算符

转载 作者:行者123 更新时间:2023-12-01 11:12:25 24 4
gpt4 key购买 nike

#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

我从 Microsoft developer docs 中获取了这个示例代码.我生成了对象 mb1 并将其复制到 mb2。然后将mb1 + mb2赋值给mb3。但是正如您在结果中看到的那样,没有调用赋值运算符。

另外,似乎临时对象是在调用加法运算符后生成的,但并未调用其析构函数。被调用的析构函数是mb1、mb2、mb3的析构函数。

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

结果:

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; 调用复制构造函数。

内存块 mb3; mb3 = mb1 + mb2; 调用赋值运算符。

关于c++ - 不调用赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58535165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com