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;
}
+37 -1
View File
@@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include "grid.h"
@@ -44,7 +45,18 @@ int main()
printArray(numbers);
printArray(numbers, size(numbers));
// "dynamic" (...) arrays
// "dynamic" (heap) arrays
int count2 = 10;
int* pNumbers = new int[count2];
pNumbers[0] = 10;
int* pNumbers2 = pNumbers; // pointer looks onto first element
for (size_t i = 0; i < count2; i++) // size can't be used for heap arrays
{
cout << pNumbers2 << ": " << *pNumbers2 << endl;
pNumbers2++;
}
delete[] pNumbers; // CAUTION - heap arrays have to be deleted with delete[]
pNumbers = pNumbers2 = nullptr;
// stl collections
vector<int> numbers5;
@@ -62,6 +74,30 @@ int main()
numbersList.push_back(10);
numbersList.push_front(0);
//numbersList[0] = 5; // random access is not available for lists
list<int>::iterator it = numbersList.begin();
for (; it != numbersList.end(); it++)
{
cout << *it << " ";
}
cout << endl;
numbersList.insert(--it, 20);
for (it = numbersList.begin(); it != numbersList.end(); it++)
{
cout << *it << " ";
}
cout << endl;
for (int n : numbersList)
{
}
for_each(numbers6.begin(), numbers6.end(), [](const int& n) {
cout << n << " ";
});
cout << endl;
return 0;
}
+14
View File
@@ -24,3 +24,17 @@ bool changeIntegerPerReference(int& n)
n *= 2;
return true;
}
bool createInteger(int* pN)
{
pN = new int(0); // BAD BAD - creates a memory leak
return false;
}
bool createIntegerWithPointerPointer(int** ppN)
{
*ppN = new int(0);
return true;
}
+4 -1
View File
@@ -3,4 +3,7 @@
void changeInteger(int n);
int changeIntegerPerReturn(int n);
bool changeIntegerPerPointer(int* pN);
bool changeIntegerPerReference(int& n);
bool changeIntegerPerReference(int& n);
bool createInteger(int* pN);
bool createIntegerWithPointerPointer(int** ppN);
+41
View File
@@ -1,4 +1,5 @@
#include <iostream>
#include <memory>
#include "functions.h"
@@ -33,5 +34,45 @@ int main()
changeIntegerPerPointer(pNumber);
changeIntegerPerReference(number);
// smart pointer
// unique_ptr - only one owner exist for the object
// shared_ptr - can have multiple owners
// weak_ptr - is a loose connection to a shared pointer
unique_ptr<int> pNumber2 = make_unique<int>(10);
*pNumber2 = 100;
//unique_ptr<int> pNumber3 = pNumber2; // compiler error- you can't create a copy of an unique pointer
unique_ptr<int> pNumber4 = make_unique<int>(*pNumber2);
shared_ptr<int> pNumber5 = make_shared<int>(10);
shared_ptr<int> pNumber6 = pNumber5;
*pNumber6 = 100;
pNumber5.reset();
weak_ptr<int> pNumber7 = pNumber6;
//*pNumber7 = 1000; // compiler error, weak pointer has no direct access to a shared pointer, you have to create a new shared pointer
if (!pNumber7.expired())
{
shared_ptr<int> tmpSharedNumber = pNumber7.lock();
*tmpSharedNumber = 10000;
}
pNumber6.reset();
shared_ptr<int> tmpSharedNumber2 = pNumber7.lock();
if (tmpSharedNumber2 != nullptr) *tmpSharedNumber2 = 100000;
// pointer-pointer
createInteger(pNumber);
createIntegerWithPointerPointer(&pNumber);
if (pNumber != nullptr)
{
delete pNumber;
pNumber = nullptr;
}
return 0;
}