#include <iostream>
#include <string>
class MyClass {
public:
std::string data;
// Constructor
MyClass(const std::string& str) : data(str) {
std::cout << "Constructed\n";
}
// Move Constructor
MyClass(MyClass&& other) noexcept : data(std::move(other.data)) {
std::cout << "Move Constructed\n";
}
// Move Assignment Operator
MyClass& operator=(MyClass&& other) noexcept {
if (this != &other) {
data = std::move(other.data); // Move the data
std::cout << "Move Assigned\n";
}
return *this;
}
// Copy Constructor
MyClass(const MyClass& other) : data(other.data) {
std::cout << "Copy Constructed\n";
}
// Copy Assignment Operator
MyClass& operator=(const MyClass& other) {
if (this != &other) {
data = other.data;
std::cout << "Copy Assigned\n";
}
return *this;
}
};
int main() {
MyClass obj1("Hello, World!");
MyClass obj2("Goodbye!");
obj2 = std::move(obj1); // Move assignment is called
std::cout << "obj1.data: " << obj1.data << "\n"; // obj1 is in a valid but unspecified state
std::cout << "obj2.data: " << obj2.data << "\n";
return 0;
}