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/MateriaSource.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'cpp04/ex03/MateriaSource.cpp') diff --git a/cpp04/ex03/MateriaSource.cpp b/cpp04/ex03/MateriaSource.cpp index 087af3e..774506b 100644 --- a/cpp04/ex03/MateriaSource.cpp +++ b/cpp04/ex03/MateriaSource.cpp @@ -6,28 +6,46 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 17:19:10 by charles #+# #+# */ -/* Updated: 2020/11/12 15:45:23 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 14:40:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "MateriaSource.hpp" -MateriaSource::MateriaSource() {} +MateriaSource::MateriaSource() : m_learned_size(0) {} -MateriaSource::MateriaSource(MateriaSource const& other) { *this = other; } +MateriaSource::MateriaSource(MateriaSource const& other) + : m_learned_size(0) { *this = other; } -void MateriaSource::operator=(MateriaSource const& other) +MateriaSource& MateriaSource::operator=(MateriaSource const& other) { + destroyLearned(); + m_learned_size = other.m_learned_size; + for (int i = 0; i < m_learned_size; i++) + m_learned[i] = other.m_learned[i]->clone(); return *this; } -MateriaSource::~MateriaSource() {} +MateriaSource::~MateriaSource() { destroyLearned(); } void MateriaSource::learnMateria(AMateria* materia) { + if (m_learned_size >= 4) + return; + m_learned[m_learned_size] = materia->clone(); + m_learned_size++; } AMateria* MateriaSource::createMateria(std::string const& type) { + for (int i = 0; i < m_learned_size; i++) + if (m_learned[i]->getType() == type) + return m_learned[i]->clone(); return NULL; } + +void MateriaSource::destroyLearned() +{ + for (int i = 0; i < m_learned_size; i++) + delete m_learned[i]; +} -- cgit