Make main not throw away temp objects on heap
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 14:49:18 +0000 (16:49 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 25 Oct 2024 14:49:18 +0000 (16:49 +0200)
The subject says that learnMateria "Copies the Materia passed as
a parameter and store it in memory so it can be cloned later."
From that I get that learnMateria makes a copy of the Materia passed
meaning we cant use "new AMateria()" to learn it, as we have no handle
to that object to delete it.

ex03/main.cpp

index 3518a3bf1aab63ab1b69419eb1fc59214525110e..bffc688bed5d75038a6142f325e38c4f414b942f 100644 (file)
@@ -13,11 +13,11 @@ int main()
        ICharacter*             bob = new Character("bob");
        AMateria*               tmp;
 
-       src->learnMateria(new Ice());
-       src->learnMateria(new Cure());
        tmp = src->createMateria("ice");
+       src->learnMateria(tmp);
        me->equip(tmp);
        tmp = src->createMateria("cure");
+       src->learnMateria(tmp);
        me->equip(tmp);
        me->use(0, *bob);
        me->use(1, *bob);