From 4c30e98f4a4018a25d3a9f3ee790d589be803cb0 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 13 Nov 2020 14:43:17 +0100 Subject: Added main for cpp04/ex01 and cpp04/ex02, Fixed cpp04/03 MateriaSource --- cpp04/ex03/Character.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'cpp04/ex03/Character.cpp') diff --git a/cpp04/ex03/Character.cpp b/cpp04/ex03/Character.cpp index ee03e63..d159aaf 100644 --- a/cpp04/ex03/Character.cpp +++ b/cpp04/ex03/Character.cpp @@ -6,20 +6,18 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 16:45:54 by charles #+# #+# */ -/* Updated: 2020/11/12 15:43:14 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:20:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "Character.hpp" -Character::Character() : m_name(""), m_inventory_size(0) {} - -Character::Character(Character const& other) { *this = other; } +Character::Character(Character const& other) + : m_inventory_size(0) { *this = other; } Character& Character::operator=(Character const& other) { - for (int i = 0; i < m_inventory_size; i++) - delete m_inventory[i]; + destroyInventory(); m_inventory_size = other.m_inventory_size; for (int i = 0; i < m_inventory_size; i++) m_inventory[i] = other.m_inventory[i]->clone(); @@ -27,11 +25,7 @@ Character& Character::operator=(Character const& other) return *this; } -Character::~Character() -{ - for (int i = 0; i < m_inventory_size; i++) - delete m_inventory[i]; -} +Character::~Character() { destroyInventory(); } Character::Character(std::string const& name) : m_name(name), m_inventory_size(0) {} @@ -60,3 +54,11 @@ void Character::use(int idx, ICharacter& target) return; m_inventory[idx]->use(target); } + +void Character::destroyInventory() +{ + for (int i = 0; i < m_inventory_size; i++) + delete m_inventory[i]; +} + +Character::Character() {} -- cgit