add casts

add inline
add template
This commit is contained in:
2025-09-23 18:17:48 +02:00
parent 3b35b973c9
commit bc4b9f1691
9 changed files with 142 additions and 3 deletions
+2
View File
@@ -180,12 +180,14 @@
<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="Troll.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Entity.h" /> <ClInclude Include="Entity.h" />
<ClInclude Include="ICompareable.h" /> <ClInclude Include="ICompareable.h" />
<ClInclude Include="Monster.h" /> <ClInclude Include="Monster.h" />
<ClInclude Include="Ork.h" /> <ClInclude Include="Ork.h" />
<ClInclude Include="Troll.h" />
<ClInclude Include="Vector3.h" /> <ClInclude Include="Vector3.h" />
<ClInclude Include="Weapon.h" /> <ClInclude Include="Weapon.h" />
</ItemGroup> </ItemGroup>
@@ -27,6 +27,9 @@
<ClCompile Include="Ork.cpp"> <ClCompile Include="Ork.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Troll.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Entity.h"> <ClInclude Include="Entity.h">
@@ -47,5 +50,8 @@
<ClInclude Include="Weapon.h"> <ClInclude Include="Weapon.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Troll.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
+2 -2
View File
@@ -8,8 +8,8 @@ public:
Entity(std::string name); Entity(std::string name);
virtual std::string toString() { return "Entity"; } // declared & defined, only use that for one liners virtual std::string toString() { return "Entity"; } // declared & defined, only use that for one liners
std::string getName() { return _name; } const std::string& getName() const { return _name; }
void setName(std::string name) { _name = name; } void setName(const std::string& name) { _name = name; }
protected: protected:
std::string _name = ""; std::string _name = "";
+5
View File
@@ -0,0 +1,5 @@
#include "Troll.h"
void Troll::Move(Vector3 move)
{
}
+11
View File
@@ -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;
};
+53
View File
@@ -5,6 +5,7 @@
#include "Vector3.h" #include "Vector3.h"
#include "ICompareable.h" #include "ICompareable.h"
#include "Ork.h" #include "Ork.h"
#include "Troll.h"
using namespace std; using namespace std;
@@ -45,5 +46,57 @@ int main()
Ork ork2 = ork; // should be a individual instances with their own values and weapons 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<unsigned short>(number);
//pSmallPositiveNumber = static_cast<unsigned short*>(pNumber);
fNumber = static_cast<float>(number);
//pFNumber = static_cast<float*>(pNumber);
//ppVoids = static_cast<void**>(&pNumbers);
pOrk = static_cast<Ork*>(pMonster);
//Weapon* weapon = static_cast<Weapon*>(pMonster);
Monster* pMonster2 = new Troll();
pOrk = static_cast<Ork*>(pMonster2); // is running, but it is not the same type
toString = pOrk->toString();
// dynamic cast
pOrk = dynamic_cast<Ork*>(pMonster2); // better use for polymorphic cast, because of runtime check
//toString = pOrk->toString();
// reinterpret cast
//smallPositiveNumber = reinterpret_cast<unsigned short>(number);
pSmallPositiveNumber = reinterpret_cast<unsigned short*>(pNumber);
pFNumber = reinterpret_cast<float*>(pNumber);
// const cast
const char* sz = "Test";
//char* sz2 = sz;
char* sz2 = const_cast<char*>(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; return 0;
} }
+24
View File
@@ -31,6 +31,8 @@ int main()
int grid[COLUMNS * ROWS] = {}; int grid[COLUMNS * ROWS] = {};
grid[calculateIndex(3, 2)] = 10; // 4th column, 3rd row grid[calculateIndex(3, 2)] = 10; // 4th column, 3rd row
//int grid2[10][10];
// size has to be always constant // size has to be always constant
const int count = 10; const int count = 10;
int numbers4[count] = {}; int numbers4[count] = {};
@@ -58,6 +60,28 @@ int main()
delete[] pNumbers; // CAUTION - heap arrays have to be deleted with delete[] delete[] pNumbers; // CAUTION - heap arrays have to be deleted with delete[]
pNumbers = pNumbers2 = nullptr; 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 // stl collections
vector<int> numbers5; vector<int> numbers5;
vector<int> numbers6 = { 0, 1, 2, 3, 4 }; vector<int> numbers6 = { 0, 1, 2, 3, 4 };
+8
View File
@@ -7,3 +7,11 @@ bool changeIntegerPerReference(int& n);
bool createInteger(int* pN); bool createInteger(int* pN);
bool createIntegerWithPointerPointer(int** ppN); bool createIntegerWithPointerPointer(int** ppN);
template<typename T>
void safeDelete(T*& pObj)
{
if (pObj == nullptr) return;
delete pObj;
pObj = nullptr;
}
+30
View File
@@ -26,6 +26,8 @@ int main()
pNumber = nullptr; pNumber = nullptr;
} }
safeDelete(pNumber);
// pointer as references outside local scope // pointer as references outside local scope
int number = 10; int number = 10;
changeInteger(number); changeInteger(number);
@@ -74,5 +76,33 @@ int main()
pNumber = nullptr; 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; return 0;
} }