Debug Move - Custom Type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <utility>
class MyType {
public:
MyType(std::string str) : mName(std::move(str)) {
std::cout << "MyType::MyType " << mName << '\n';
}
~MyType() {
std::cout << "MyType::~MyType " << mName << '\n';
}
MyType(const MyType& other) : mName(other.mName) {
std::cout << "MyType::MyType(const MyType&) " << mName << '\n';
}
MyType(MyType&& other) noexcept : mName(std::move(other.mName)) {
std::cout << "MyType::MyType(MyType&&) " << mName << '\n';
}
MyType& operator=(const MyType& other) {
if (this != &other)
mName = other.mName;
std::cout << "MyType::operator=(const MyType&) " << mName << '\n';
return *this;
}
MyType& operator=(MyType&& other) noexcept {
if (this != &other)
mName = std::move(other.mName);
std::cout << "MyType::operator=(MyType&&) " << mName << '\n';
return *this;
}
private:
std::string mName;
};
int main()
{
{
MyType type("ABC");
auto tmoved = std::move(type);
}
{
MyType tassigned("XYZ");
MyType temp("ABC");
tassigned = std::move(temp);
Enter to Rename, Shift+Enter to Preview