From 01862f6b40c0f8fbc0123f1ac5140a406c268251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 10 Nov 2020 13:42:50 +0100 Subject: destructor and copy assignment operator --- labb4/GameState.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'labb4/GameState.cpp') diff --git a/labb4/GameState.cpp b/labb4/GameState.cpp index 6de11dd..47842a1 100644 --- a/labb4/GameState.cpp +++ b/labb4/GameState.cpp @@ -27,6 +27,28 @@ GameState::GameState(const GameState &other) { hero = other.hero; } +/* +*/ +GameState::~GameState() { + for (const auto &robot : robots) { + delete robot; + } + robots.clear(); +} + +GameState &GameState::operator=(const GameState &other) { + for (const auto &robot : robots) { + delete robot; + } + robots.clear(); + for (const auto &robot : other.robots) { + robots.push_back(robot->clone()); + } + hero = other.hero; + + return *this; +} + void GameState::draw(QGraphicsScene *scene) const { scene->clear(); for (int i = 0; i < robots.size(); i++) { -- cgit v1.2.1