diff --git a/CPlusPlus/Classes/Classes.vcxproj b/CPlusPlus/Classes/Classes.vcxproj
index 9709639..b7bcb5c 100644
--- a/CPlusPlus/Classes/Classes.vcxproj
+++ b/CPlusPlus/Classes/Classes.vcxproj
@@ -180,12 +180,14 @@
+
+
diff --git a/CPlusPlus/Classes/Classes.vcxproj.filters b/CPlusPlus/Classes/Classes.vcxproj.filters
index 1ce6882..d56fba5 100644
--- a/CPlusPlus/Classes/Classes.vcxproj.filters
+++ b/CPlusPlus/Classes/Classes.vcxproj.filters
@@ -27,6 +27,9 @@
Source Files
+
+ Source Files
+
@@ -47,5 +50,8 @@
Header Files
+
+ Header Files
+
\ No newline at end of file
diff --git a/CPlusPlus/Classes/Entity.h b/CPlusPlus/Classes/Entity.h
index 10e756b..a180051 100644
--- a/CPlusPlus/Classes/Entity.h
+++ b/CPlusPlus/Classes/Entity.h
@@ -8,8 +8,8 @@ public:
Entity(std::string name);
virtual std::string toString() { return "Entity"; } // declared & defined, only use that for one liners
- std::string getName() { return _name; }
- void setName(std::string name) { _name = name; }
+ const std::string& getName() const { return _name; }
+ void setName(const std::string& name) { _name = name; }
protected:
std::string _name = "";
diff --git a/CPlusPlus/Classes/Troll.cpp b/CPlusPlus/Classes/Troll.cpp
new file mode 100644
index 0000000..e3080b9
--- /dev/null
+++ b/CPlusPlus/Classes/Troll.cpp
@@ -0,0 +1,5 @@
+#include "Troll.h"
+
+void Troll::Move(Vector3 move)
+{
+}
diff --git a/CPlusPlus/Classes/Troll.h b/CPlusPlus/Classes/Troll.h
new file mode 100644
index 0000000..be37ec3
--- /dev/null
+++ b/CPlusPlus/Classes/Troll.h
@@ -0,0 +1,11 @@
+#pragma once
+#include "Monster.h"
+
+class Troll : public Monster
+{
+ std::string toString() override { return "Troll"; }
+
+ // Inherited via Monster
+ void Move(Vector3 move) override;
+};
+
diff --git a/CPlusPlus/Classes/main.cpp b/CPlusPlus/Classes/main.cpp
index e84db92..9cbc527 100644
--- a/CPlusPlus/Classes/main.cpp
+++ b/CPlusPlus/Classes/main.cpp
@@ -5,6 +5,7 @@
#include "Vector3.h"
#include "ICompareable.h"
#include "Ork.h"
+#include "Troll.h"
using namespace std;
@@ -45,5 +46,57 @@ int main()
Ork ork2 = ork; // should be a individual instances with their own values and weapons
+ // casts
+ // c cast
+ int number = -10000000;
+ unsigned short smallPositiveNumber = (unsigned short)number;
+ int* pNumber = &number;
+ unsigned short* pSmallPositiveNumber = (unsigned short*)pNumber;
+ float fNumber = (float)number;
+ float* pFNumber = (float*)pNumber;
+ int* pNumbers = new int[10];
+ void** ppVoids = (void**)&pNumbers;
+ Monster* pMonster = new Ork();
+ Ork* pOrk = (Ork*)pMonster;
+ std::string toString = pOrk->toString();
+
+ // static_cast
+ smallPositiveNumber = static_cast(number);
+ //pSmallPositiveNumber = static_cast(pNumber);
+ fNumber = static_cast(number);
+ //pFNumber = static_cast(pNumber);
+ //ppVoids = static_cast(&pNumbers);
+ pOrk = static_cast(pMonster);
+ //Weapon* weapon = static_cast(pMonster);
+ Monster* pMonster2 = new Troll();
+ pOrk = static_cast(pMonster2); // is running, but it is not the same type
+ toString = pOrk->toString();
+
+ // dynamic cast
+ pOrk = dynamic_cast(pMonster2); // better use for polymorphic cast, because of runtime check
+ //toString = pOrk->toString();
+
+ // reinterpret cast
+ //smallPositiveNumber = reinterpret_cast(number);
+ pSmallPositiveNumber = reinterpret_cast(pNumber);
+ pFNumber = reinterpret_cast(pNumber);
+
+ // const cast
+ const char* sz = "Test";
+ //char* sz2 = sz;
+ char* sz2 = const_cast(sz);
+
+ const std::string str = "Test";
+ std::string str2 = str;
+ str.c_str(); // if you need a zero terminated string
+
+ delete[] pNumbers;
+ pNumbers = nullptr;
+ delete pMonster;
+ pMonster = nullptr;
+ pOrk = nullptr;
+ delete pMonster2;
+ pMonster2 = nullptr;
+
return 0;
}
\ No newline at end of file
diff --git a/CPlusPlus/Collections/main.cpp b/CPlusPlus/Collections/main.cpp
index 399d5c6..45b1f1b 100644
--- a/CPlusPlus/Collections/main.cpp
+++ b/CPlusPlus/Collections/main.cpp
@@ -31,6 +31,8 @@ int main()
int grid[COLUMNS * ROWS] = {};
grid[calculateIndex(3, 2)] = 10; // 4th column, 3rd row
+ //int grid2[10][10];
+
// size has to be always constant
const int count = 10;
int numbers4[count] = {};
@@ -58,6 +60,28 @@ int main()
delete[] pNumbers; // CAUTION - heap arrays have to be deleted with delete[]
pNumbers = pNumbers2 = nullptr;
+ // 2d heap array
+ int** pGrid = new int*[10];
+ cout << "grid starts at " << pGrid << endl;
+ for (size_t i = 0; i < 10; i++)
+ {
+ pGrid[i] = new int[10];
+ cout << i << ". row: " << &pGrid[i] << endl;
+ for (size_t j = 0; j < 10; j++)
+ {
+ cout << i << "/" << j << ": " << &pGrid[i][j] << endl;
+ }
+ }
+
+ // delete inner arrays first
+ for (size_t i = 0; i < 10; i++)
+ {
+ delete[] pGrid[i];
+ pGrid[i] = nullptr;
+ }
+ delete[] pGrid; // delete outer array second
+ pGrid = nullptr;
+
// stl collections
vector numbers5;
vector numbers6 = { 0, 1, 2, 3, 4 };
diff --git a/CPlusPlus/Pointers/functions.h b/CPlusPlus/Pointers/functions.h
index 635008d..1e0a66c 100644
--- a/CPlusPlus/Pointers/functions.h
+++ b/CPlusPlus/Pointers/functions.h
@@ -6,4 +6,12 @@ bool changeIntegerPerPointer(int* pN);
bool changeIntegerPerReference(int& n);
bool createInteger(int* pN);
-bool createIntegerWithPointerPointer(int** ppN);
\ No newline at end of file
+bool createIntegerWithPointerPointer(int** ppN);
+
+template
+void safeDelete(T*& pObj)
+{
+ if (pObj == nullptr) return;
+ delete pObj;
+ pObj = nullptr;
+}
\ No newline at end of file
diff --git a/CPlusPlus/Pointers/main.cpp b/CPlusPlus/Pointers/main.cpp
index 1d3a9c4..a6ab730 100644
--- a/CPlusPlus/Pointers/main.cpp
+++ b/CPlusPlus/Pointers/main.cpp
@@ -26,6 +26,8 @@ int main()
pNumber = nullptr;
}
+ safeDelete(pNumber);
+
// pointer as references outside local scope
int number = 10;
changeInteger(number);
@@ -74,5 +76,33 @@ int main()
pNumber = nullptr;
}
+ // const example
+ const int constNumber = 10;
+ //constNumber = 20; // a const value can not be changed
+
+ const int* constNumberPointer = new int(10);
+ //*constNumberPointer = 20; // this is a const value
+ constNumberPointer = new int(20); // but not a const pointer
+
+ int const* numberConstPointer = new int(10);
+ //*numberConstPointer = 20; // this is a const value
+ numberConstPointer = new int(20); // but not a const pointer
+
+ int* const numberPointerConst = new int(10);
+ *numberPointerConst = 20; // the value is not const
+ //numberPointerConst = new int(20); // but the pointer is const
+
+ const int* const constNumberPointerConst = new int(10);
+ //*constNumberPointerConst = 20; // this is a const value
+ //constNumberPointerConst = new int(20); // and the pointer as well
+
+ int const* const numberConstPointerConst = new int(10);
+ //*numberConstPointerConst = 20; // this is a const value
+ //numberConstPointerConst = new int(20); // and the pointer as well
+
+ const int const* const constNumberConstPointerConst = new int(10);
+ //*constNumberConstPointerConst = 20; // this is a const value
+ //constNumberConstPointerConst = new int(20); // and the pointer as well
+
return 0;
}
\ No newline at end of file