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
+2
View File
@@ -187,6 +187,7 @@
<ClCompile Include="main.cpp" />
<ClCompile Include="Monster.cpp" />
<ClCompile Include="Ork.cpp" />
<ClCompile Include="Weapon.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Entity.h" />
@@ -194,6 +195,7 @@
<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">
@@ -27,6 +27,9 @@
<ClCompile Include="Ork.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Weapon.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Entity.h">
@@ -44,5 +47,8 @@
<ClInclude Include="Vector3.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Weapon.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
+4 -1
View File
@@ -3,16 +3,19 @@
Entity::Entity()
{
std::cout << "Create Entity" << std::endl;
}
Entity::Entity(std::string name) : name(name)
{
std::cout << "Create Entity with name" << std::endl;
//this->name = name;
}
Entity::~Entity()
{
std::cout << "Destroy Entity" << std::endl;
}
std::string Entity::ToString()
+7 -1
View File
@@ -1,9 +1,15 @@
#pragma once
// only for presentation
#include <iostream>
class ICompareable
{
public:
virtual ~ICompareable() {}
// only for presentation
ICompareable() { std::cout << "Create IComparable" << std::endl; }
virtual ~ICompareable() { std::cout << "Destroy IComparable" << std::endl; }
virtual int Compare(ICompareable* other) = 0;
};
+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)
+7
View File
@@ -1,6 +1,8 @@
#pragma once
#include "Entity.h"
#include "ICompareable.h"
#include "Weapon.h"
//enum Monsters
//{
@@ -12,6 +14,9 @@
class Monster : public Entity, public ICompareable
{
public:
// internal pointers often requires a default & copy constructor and a destructor
Monster();
Monster(const Monster& other);
Monster(int strength);
virtual ~Monster();
@@ -22,6 +27,8 @@ public:
int Compare(ICompareable* other) override;
protected:
Weapon* _pWeapon = nullptr;
int _strength = 0;
float _speed = 0.0f;
};
+8 -3
View File
@@ -1,19 +1,24 @@
#include "Ork.h"
#include <iostream>
Ork::Ork() : Monster(10)
{
std::cout << "Create Ork" << std::endl;
//this->_strength = 10;
this->name = "My Ork";
}
Ork::Ork(const Ork& other) : Monster(other._strength)
Ork::Ork(const Ork& other) : Monster(other)
{
this->name = other.name;
this->_speed = other._speed;
std::cout << "Create a copy of an Ork" << std::endl;
//this->name = other.name;
//this->_speed = other._speed;
}
Ork::~Ork()
{
std::cout << "Destroy Ork" << std::endl;
}
void Ork::Move()
+1
View File
@@ -0,0 +1 @@
#include "Weapon.h"
+5
View File
@@ -0,0 +1,5 @@
#pragma once
class Weapon
{
};
+25 -21
View File
@@ -8,38 +8,42 @@ using namespace std;
int main()
{
Entity entity; // this is an entity instance
entity.name = "My Entity";
//Entity entity; // this is an entity instance
//entity.name = "My Entity";
//Monster monster; // can't be instantiated, because Monster is abstract
//Monster monster2 = Monster::Create(Monsters::Ork);
Ork ork;
ork.name = "My Ork";
ork.name = "Ork1";
ork.Move();
cout << entity.ToString() << endl;
cout << ork.ToString() << endl;
//cout << entity.ToString() << endl;
//cout << ork.ToString() << endl;
Vector3 position(1.0f, 3.0f, -5.0f);
position.y = 0.0f;
//Vector3 position(1.0f, 3.0f, -5.0f);
//position.y = 0.0f;
ICompareable* pComparable = nullptr;
pComparable = &ork;
pComparable->Compare(&ork);
(*pComparable).Compare(&ork); // same as line above, but more complicated to write
//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(nullptr);
delete pComparables[i];
pComparables[i] = nullptr;
}
//ICompareable* pComparables[] = { new Ork, new Ork, new Ork };
//for (size_t i = 0; i < size(pComparables); i++)
//{
// //pComparables[i]->Compare(nullptr);
// delete pComparables[i];
// pComparables[i] = nullptr;
//}
ICompareable** pHeapComparables = new ICompareable*[10];
delete[] pHeapComparables;
pHeapComparables = nullptr;
//ICompareable** pHeapComparables = new ICompareable*[10];
//delete[] pHeapComparables;
//pHeapComparables = nullptr;
//Ork ork2(ork);
//ork2.name = "Ork2";
//ork2.Move();
return 0;
}
+69 -5
View File
@@ -1,4 +1,5 @@
#include <iostream>
#include <memory> // smart pointer header
using namespace std;
@@ -7,6 +8,9 @@ void changeIntegerPerPointer(int* pN);
int changeIntegerPerReturn(int n);
void changeIntegerPerReference(int& n);
bool createInteger(int* pN);
bool createIntegerWithPointerPointer(int** ppN);
int main()
{
// general
@@ -29,11 +33,57 @@ int main()
}
// pointer as references
int number = 0;
changeInteger(number);
changeIntegerPerPointer(&number);
changeIntegerPerPointer(pNumber);
changeIntegerPerReference(number);
//int number = 0;
//changeInteger(number);
//changeIntegerPerPointer(&number);
//changeIntegerPerPointer(pNumber);
//changeIntegerPerReference(number);
// smart pointer
// unique_ptr -> only one owner exist for the object
// shared_ptr -> can have multiple owners
// weak_ptr -> is loose connection to a shared_ptr
unique_ptr<int> number = make_unique<int>(10);
*number = 100;
//unique_ptr<int> number2 = number; // compiler error - you can create a copy of an unique pointer
unique_ptr<int> number2 = make_unique<int>(*number);
shared_ptr<int> number3 = make_shared<int>(20);
shared_ptr<int> number4 = number3;
*number3 = 200;
*number4 = 2000;
number3.reset();
weak_ptr<int> weakNumber = number4;
//*weakNumber = 20000; // compiler error, weak pointer have no direct access to a shared pointer, you have to create one
if (!weakNumber.expired())
{
shared_ptr<int> tmpSharedNumber = weakNumber.lock();
*tmpSharedNumber = 20000;
}
number4.reset();
shared_ptr<int> tmpSharedNumber = weakNumber.lock();
// pointer-pointer
if (createInteger(pNumber))
{
cout << "Create integer was succesful" << endl;
}
if (createIntegerWithPointerPointer(&pNumber))
{
cout << "Create integer was succesful" << endl;
}
if (pNumber != nullptr)
{
delete pNumber;
pNumber = nullptr;
}
return 0;
}
@@ -56,3 +106,17 @@ void changeIntegerPerReference(int& n)
{
n = 300;
}
bool createInteger(int* pN)
{
pN = new int(0); // CAUTION - creates a MEMORY LEAK
return false;
}
bool createIntegerWithPointerPointer(int** ppN)
{
*ppN = new int(0);
return true;
}