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:
@@ -187,6 +187,7 @@
|
|||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="Monster.cpp" />
|
<ClCompile Include="Monster.cpp" />
|
||||||
<ClCompile Include="Ork.cpp" />
|
<ClCompile Include="Ork.cpp" />
|
||||||
|
<ClCompile Include="Weapon.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Entity.h" />
|
<ClInclude Include="Entity.h" />
|
||||||
@@ -194,6 +195,7 @@
|
|||||||
<ClInclude Include="Monster.h" />
|
<ClInclude Include="Monster.h" />
|
||||||
<ClInclude Include="Ork.h" />
|
<ClInclude Include="Ork.h" />
|
||||||
<ClInclude Include="Vector3.h" />
|
<ClInclude Include="Vector3.h" />
|
||||||
|
<ClInclude Include="Weapon.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
@@ -27,6 +27,9 @@
|
|||||||
<ClCompile Include="Ork.cpp">
|
<ClCompile Include="Ork.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Weapon.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Entity.h">
|
<ClInclude Include="Entity.h">
|
||||||
@@ -44,5 +47,8 @@
|
|||||||
<ClInclude Include="Vector3.h">
|
<ClInclude Include="Vector3.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Weapon.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -3,16 +3,19 @@
|
|||||||
|
|
||||||
Entity::Entity()
|
Entity::Entity()
|
||||||
{
|
{
|
||||||
|
std::cout << "Create Entity" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Entity::Entity(std::string name) : name(name)
|
Entity::Entity(std::string name) : name(name)
|
||||||
{
|
{
|
||||||
|
std::cout << "Create Entity with name" << std::endl;
|
||||||
|
|
||||||
//this->name = name;
|
//this->name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
Entity::~Entity()
|
Entity::~Entity()
|
||||||
{
|
{
|
||||||
|
std::cout << "Destroy Entity" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Entity::ToString()
|
std::string Entity::ToString()
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// only for presentation
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
class ICompareable
|
class ICompareable
|
||||||
{
|
{
|
||||||
public:
|
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;
|
virtual int Compare(ICompareable* other) = 0;
|
||||||
};
|
};
|
||||||
@@ -2,13 +2,40 @@
|
|||||||
|
|
||||||
#include "Monster.h"
|
#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";
|
this->name = "My Monster";
|
||||||
}
|
}
|
||||||
|
|
||||||
Monster::~Monster()
|
Monster::~Monster()
|
||||||
{
|
{
|
||||||
|
std::cout << "Destroy Monster" << std::endl;
|
||||||
|
|
||||||
|
if (_pWeapon != nullptr)
|
||||||
|
{
|
||||||
|
delete _pWeapon;
|
||||||
|
_pWeapon = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Monster Monster::Create(Monsters type)
|
//Monster Monster::Create(Monsters type)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
#include "ICompareable.h"
|
#include "ICompareable.h"
|
||||||
|
#include "Weapon.h"
|
||||||
|
|
||||||
//enum Monsters
|
//enum Monsters
|
||||||
//{
|
//{
|
||||||
@@ -12,6 +14,9 @@
|
|||||||
class Monster : public Entity, public ICompareable
|
class Monster : public Entity, public ICompareable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
// internal pointers often requires a default & copy constructor and a destructor
|
||||||
|
Monster();
|
||||||
|
Monster(const Monster& other);
|
||||||
Monster(int strength);
|
Monster(int strength);
|
||||||
virtual ~Monster();
|
virtual ~Monster();
|
||||||
|
|
||||||
@@ -22,6 +27,8 @@ public:
|
|||||||
int Compare(ICompareable* other) override;
|
int Compare(ICompareable* other) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Weapon* _pWeapon = nullptr;
|
||||||
|
|
||||||
int _strength = 0;
|
int _strength = 0;
|
||||||
float _speed = 0.0f;
|
float _speed = 0.0f;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,19 +1,24 @@
|
|||||||
#include "Ork.h"
|
#include "Ork.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
Ork::Ork() : Monster(10)
|
Ork::Ork() : Monster(10)
|
||||||
{
|
{
|
||||||
|
std::cout << "Create Ork" << std::endl;
|
||||||
|
|
||||||
//this->_strength = 10;
|
//this->_strength = 10;
|
||||||
this->name = "My Ork";
|
this->name = "My Ork";
|
||||||
}
|
}
|
||||||
|
|
||||||
Ork::Ork(const Ork& other) : Monster(other._strength)
|
Ork::Ork(const Ork& other) : Monster(other)
|
||||||
{
|
{
|
||||||
this->name = other.name;
|
std::cout << "Create a copy of an Ork" << std::endl;
|
||||||
this->_speed = other._speed;
|
//this->name = other.name;
|
||||||
|
//this->_speed = other._speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ork::~Ork()
|
Ork::~Ork()
|
||||||
{
|
{
|
||||||
|
std::cout << "Destroy Ork" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ork::Move()
|
void Ork::Move()
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
#include "Weapon.h"
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
class Weapon
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
+25
-21
@@ -8,38 +8,42 @@ using namespace std;
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Entity entity; // this is an entity instance
|
//Entity entity; // this is an entity instance
|
||||||
entity.name = "My Entity";
|
//entity.name = "My Entity";
|
||||||
|
|
||||||
//Monster monster; // can't be instantiated, because Monster is abstract
|
//Monster monster; // can't be instantiated, because Monster is abstract
|
||||||
//Monster monster2 = Monster::Create(Monsters::Ork);
|
//Monster monster2 = Monster::Create(Monsters::Ork);
|
||||||
|
|
||||||
Ork ork;
|
Ork ork;
|
||||||
ork.name = "My Ork";
|
ork.name = "Ork1";
|
||||||
ork.Move();
|
ork.Move();
|
||||||
|
|
||||||
cout << entity.ToString() << endl;
|
//cout << entity.ToString() << endl;
|
||||||
cout << ork.ToString() << endl;
|
//cout << ork.ToString() << endl;
|
||||||
|
|
||||||
Vector3 position(1.0f, 3.0f, -5.0f);
|
//Vector3 position(1.0f, 3.0f, -5.0f);
|
||||||
position.y = 0.0f;
|
//position.y = 0.0f;
|
||||||
|
|
||||||
ICompareable* pComparable = nullptr;
|
//ICompareable* pComparable = nullptr;
|
||||||
pComparable = &ork;
|
//pComparable = &ork;
|
||||||
pComparable->Compare(&ork);
|
//pComparable->Compare(&ork);
|
||||||
(*pComparable).Compare(&ork); // same as line above, but more complicated to write
|
//(*pComparable).Compare(&ork); // same as line above, but more complicated to write
|
||||||
|
|
||||||
ICompareable* pComparables[] = { new Ork, new Ork, new Ork };
|
//ICompareable* pComparables[] = { new Ork, new Ork, new Ork };
|
||||||
for (size_t i = 0; i < size(pComparables); i++)
|
//for (size_t i = 0; i < size(pComparables); i++)
|
||||||
{
|
//{
|
||||||
//pComparables[i]->Compare(nullptr);
|
// //pComparables[i]->Compare(nullptr);
|
||||||
delete pComparables[i];
|
// delete pComparables[i];
|
||||||
pComparables[i] = nullptr;
|
// pComparables[i] = nullptr;
|
||||||
}
|
//}
|
||||||
|
|
||||||
ICompareable** pHeapComparables = new ICompareable*[10];
|
//ICompareable** pHeapComparables = new ICompareable*[10];
|
||||||
delete[] pHeapComparables;
|
//delete[] pHeapComparables;
|
||||||
pHeapComparables = nullptr;
|
//pHeapComparables = nullptr;
|
||||||
|
|
||||||
|
//Ork ork2(ork);
|
||||||
|
//ork2.name = "Ork2";
|
||||||
|
//ork2.Move();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory> // smart pointer header
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -7,6 +8,9 @@ void changeIntegerPerPointer(int* pN);
|
|||||||
int changeIntegerPerReturn(int n);
|
int changeIntegerPerReturn(int n);
|
||||||
void changeIntegerPerReference(int& n);
|
void changeIntegerPerReference(int& n);
|
||||||
|
|
||||||
|
bool createInteger(int* pN);
|
||||||
|
bool createIntegerWithPointerPointer(int** ppN);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// general
|
// general
|
||||||
@@ -18,7 +22,7 @@ int main()
|
|||||||
|
|
||||||
*pNumber = 567;
|
*pNumber = 567;
|
||||||
|
|
||||||
//pNumber = new int(0); // BAD BAD - Memory Leak, because we override an existing pointer address with a new one without saving the old one
|
// pNumber = new int(0); // BAD BAD - Memory Leak, because we override an existing pointer address with a new one without saving the old one
|
||||||
|
|
||||||
// here are 100s of lines of code
|
// here are 100s of lines of code
|
||||||
|
|
||||||
@@ -29,11 +33,57 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// pointer as references
|
// pointer as references
|
||||||
int number = 0;
|
//int number = 0;
|
||||||
changeInteger(number);
|
//changeInteger(number);
|
||||||
changeIntegerPerPointer(&number);
|
//changeIntegerPerPointer(&number);
|
||||||
changeIntegerPerPointer(pNumber);
|
//changeIntegerPerPointer(pNumber);
|
||||||
changeIntegerPerReference(number);
|
//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;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -56,3 +106,17 @@ void changeIntegerPerReference(int& n)
|
|||||||
{
|
{
|
||||||
n = 300;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user