#include #include "main.hpp" Base *DerivedA::clone() { std::cout << "clone da" << std::endl; return new DerivedA; } Base *DerivedB::clone() { std::cout << "clone db" << std::endl; return new DerivedB; } void Base::hello() { std::cout << 0 << std::endl; } void DerivedA::hello() { std::cout << 1 << std::endl; } void DerivedB::hello() { std::cout << 2 << std::endl; } GameState::GameState() { std::cout << "c" << std::endl; thing = new DerivedA; } GameState::GameState(const GameState &other) { std::cout << "cc" << std::endl; thing = other.thing->clone(); } GameState &GameState::operator=(const GameState &other) { std::cout << "=" << std::endl; thing = other.thing; return *this; } GameState::~GameState() { std::cout << "d gamestate" << std::endl; delete thing; thing = nullptr; } void GameState::switchThing() { std::cout << "switch" << std::endl; delete thing; thing = new DerivedB; } int main(int argc, char *argv[]) { GameState state; state.thing->hello(); std::cout << std::endl; GameState copy = state; state.thing->hello(); copy.thing->hello(); std::cout << std::endl; copy.switchThing(); state.thing->hello(); copy.thing->hello(); std::cout << std::endl; state = copy; state.thing->hello(); copy.thing->hello(); return 0; }