add smart pointers

add polymorphism
add destructor & copy constructor
add pointer-pointer
This commit is contained in:
2025-09-22 18:22:03 +02:00
parent d8f6014fab
commit 3b35b973c9
13 changed files with 208 additions and 7 deletions
+2
View File
@@ -183,9 +183,11 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="Entity.h" />
<ClInclude Include="ICompareable.h" />
<ClInclude Include="Monster.h" />
<ClInclude Include="Ork.h" />
<ClInclude Include="Vector3.h" />
<ClInclude Include="Weapon.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
@@ -41,5 +41,11 @@
<ClInclude Include="Vector3.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ICompareable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Weapon.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
+13
View File
@@ -0,0 +1,13 @@
#pragma once
// only for demonstration
#include <iostream>
class ICompareable
{
public:
ICompareable() { std::cout << "ICompareable::ICompareable()" << std::endl; } // only for demonstration
virtual ~ICompareable() { std::cout << "ICompareable::~ICompareable()" << std::endl; } // only for demonstration
virtual int Compare(ICompareable* pOther) = 0;
};
+30 -1
View File
@@ -1,6 +1,35 @@
#include "Monster.h"
#include <iostream>
Monster::Monster()
Monster::Monster() : _pWeapon(new Weapon())
{
//toString();
std::cout << "Monster::Monster()" << std::endl;
}
Monster::Monster(const Monster& other)
{
// shallow copy
this->_name = other._name;
//this->_pWeapon = other._pWeapon;
// deep copy
this->_pWeapon = new Weapon(*other._pWeapon);
}
Monster::~Monster()
{
std::cout << "Monster::~Monster()" << std::endl;
if (_pWeapon != nullptr)
{
delete _pWeapon;
_pWeapon = nullptr;
}
}
int Monster::Compare(ICompareable* pOther)
{
return 0;
}
+14 -1
View File
@@ -1,11 +1,24 @@
#pragma once
#include "Entity.h"
#include "ICompareable.h"
#include "Vector3.h"
#include "Weapon.h"
class Monster : public Entity
class Monster : public Entity, public ICompareable
{
public:
// default constructor, copy constructor & destructor if you need one you'll need all of them
// internal pointers often requires default & copy constructor & a destructor at the same time
Monster();
Monster(const Monster& other); // copy constructor
virtual ~Monster();
std::string toString() override { return "Monster"; }
virtual void Move(Vector3 move) = 0;
int Compare(ICompareable* pOther) override;
private:
Weapon* _pWeapon = nullptr;
};
+12
View File
@@ -1,6 +1,18 @@
#include "Ork.h"
#include <iostream>
Ork::Ork()
{
//toString();
std::cout << "Ork::Ork()" << std::endl;
}
Ork::~Ork()
{
std::cout << "Ork::~Ork()" << std::endl;
}
void Ork::Move(Vector3 move)
{
}
+4 -1
View File
@@ -4,8 +4,11 @@
class Ork : public Monster
{
public:
Ork();
Ork(); // constructor
virtual ~Ork(); // destructor
std::string toString() override { return "Ork"; }
void Move(Vector3 move) override;
};
+4
View File
@@ -0,0 +1,4 @@
#pragma once
class Weapon
{ };
+27 -2
View File
@@ -3,6 +3,8 @@
#include "Entity.h"
#include "Monster.h"
#include "Vector3.h"
#include "ICompareable.h"
#include "Ork.h"
using namespace std;
@@ -11,8 +13,9 @@ int main()
Entity entity; // this is an entity instance
cout << entity.toString() << endl;
Monster monster;
cout << monster.toString() << endl;
// can't be instantiated because of abstract method
//Monster monster;
//cout << monster.toString() << endl;
Vector3 position;
Vector3 position2(1.0f, 0.0f, -10.0f);
@@ -20,5 +23,27 @@ int main()
Vector3 position4{}; // list-initializer
Vector3 position5 = {}; // list-initializer assigned
Ork ork;
ICompareable* pComparable = nullptr;
pComparable = &ork;
pComparable->Compare(&ork);
(*pComparable).Compare(&ork); // same as line above, but more complicated to write
ICompareable* pComparables[] = { new Ork, new Ork, new Ork };
for (size_t i = 0; i < size(pComparables); i++)
{
pComparables[i]->Compare(&ork);
delete pComparables[i];
pComparables[i] = nullptr;
}
// creates an "dynamic" heap pointer array
ICompareable** pHeapComparables = new ICompareable*[10];
delete[] pHeapComparables;
pHeapComparables = nullptr;
Ork ork2 = ork; // should be a individual instances with their own values and weapons
return 0;
}