#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;
}
I took this sample code from Microsoft developer docs. I've generated the object mb1 and copied it to mb2. Then assigned mb1 + mb2 to 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. The called destructor is mb1, mb2, and mb3's one.
Why assignment operator is not called even though there is a sentence 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.