{}
run-icon
main.cpp
#include <iostream> #include <utility> // Для std::move class TestClass { public: // Конструктор по умолчанию TestClass() { std::cout << "Default constructor called" << std::endl; } // Параметризированный конструктор TestClass(int value) : value_(value) { std::cout << "Parameterized constructor called" << std::endl; } // Конструктор копирования TestClass(const TestClass& other) : value_(other.value_) { std::cout << "Copy constructor called" << std::endl; } // Конструктор перемещения TestClass(TestClass&& other) noexcept : value_(std::move(other.value_)) { std::cout << "Move constructor called" << std::endl; other.value_ = 0; // Обнуление значения у перемещенного объекта } // Оператор присваивания копированием TestClass& operator=(const TestClass& other) { std::cout << "Copy assignment operator called" << std::endl; if (this == &other) { return *this; } value_ = other.value_; return *this; } // Оператор присваивания перемещением TestClass& operator=(TestClass&& other) noexcept { std::cout << "Move assignment operator called" << std::endl; if (this == &other) { return *this; } value_ = std::move(other.value_); other.value_ = 0; // Обнуление значения у перемещенного объекта return *this; } // Деструктор ~TestClass() { std::cout << "Destructor called" << std::endl; } // Метод для получения значения int getValue() const { return value_; } private: int value_ = 0; }; static TestClass fun1() { TestClass t; return t; } int main(int argc, const char * argv[]) { TestClass t; t = fun1(); return 0; }
Output