From b8da24cd224f9c4d793bb4bb9809b6b517f0ee38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 10 Nov 2020 13:36:39 +0100 Subject: test: rule of three --- labb4/test-rule-of-three/main.cpp | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 labb4/test-rule-of-three/main.cpp (limited to 'labb4/test-rule-of-three/main.cpp') diff --git a/labb4/test-rule-of-three/main.cpp b/labb4/test-rule-of-three/main.cpp new file mode 100644 index 0000000..ccef31a --- /dev/null +++ b/labb4/test-rule-of-three/main.cpp @@ -0,0 +1,74 @@ +#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; +} -- cgit v1.2.1