diff options
Diffstat (limited to 'cpp04/ex03/MateriaSource.cpp')
| -rw-r--r-- | cpp04/ex03/MateriaSource.cpp | 28 |
1 files changed, 23 insertions, 5 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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]; +} |
