add example for "default constructor, copy constructor, destructor, if you need one you'll need all"

add smart pointer
add pointer pointer
This commit is contained in:
2025-03-27 21:37:30 +01:00
parent 4b6ac6a484
commit a557775853
11 changed files with 163 additions and 33 deletions
+28 -1
View File
@@ -2,13 +2,40 @@
#include "Monster.h"
Monster::Monster(int strength) : Entity("My Monster"), _strength(strength)
Monster::Monster() : Entity("My Monster"), _pWeapon(new Weapon())
{
std::cout << "Create Monster" << std::endl;
}
Monster::Monster(const Monster& other) : Entity(other), ICompareable(other)
{
std::cout << "Create Copy of a Monster" << std::endl;
// shallow copy
//this->_pWeapon = other._pWeapon;
this->_strength = other._strength;
this->_speed = other._speed;
// deep copy of internal pointers
this->_pWeapon = new Weapon(*other._pWeapon);
}
Monster::Monster(int strength) : Entity("My Monster"), _strength(strength), _pWeapon(new Weapon())
{
std::cout << "Create Monster with strength" << std::endl;
this->name = "My Monster";
}
Monster::~Monster()
{
std::cout << "Destroy Monster" << std::endl;
if (_pWeapon != nullptr)
{
delete _pWeapon;
_pWeapon = nullptr;
}
}
//Monster Monster::Create(Monsters type)