102 lines
2.8 KiB
C++
102 lines
2.8 KiB
C++
#include <iostream>
|
|
|
|
#include "Entity.h"
|
|
#include "Monster.h"
|
|
#include "Vector3.h"
|
|
#include "ICompareable.h"
|
|
#include "Ork.h"
|
|
#include "Troll.h"
|
|
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
Entity entity; // this is an entity instance
|
|
cout << entity.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);
|
|
Vector3 position3 = Vector3();
|
|
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
|
|
|
|
// 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;
|
|
} |