I am quite new to c++ and I am just trying to get to grips with the pointers and syntaxes that it uses. I have been using for about 6 weeks and I am making a small game engine on it just to try and get better at programming. I currently have a list that will hold Transforms, a superclass that i have made to hold objects basic characteristics. I have Two subclasses for my transform, GameObject and Primitive.
Primitives are basic shapes such as planes and cubes where as GameObjects are objects that contain children and a parent. I have made it in such a way that transforms can be parented and have children so both of these subclasses can be connected for moving transformations and such.
This all works through polymorphism but when i try to call the renderer on my list of transforms it will call the superclass version of Update that resides in transform. Which it shouldn't in theory as in other languages. So I have had to try and cast them to their respective classes, which is a little messier but I have run into a problem.
Its irritating that i cant just do casts like (Primtive)transform. It also screws up the variable primitiveType, it makes it some weird large number.
for (Transform go : gameObjects)
{
if (go.type == TYPE_MESH)
{
Transform* t = new Transform();
t = &go;
Primitive* primitive = static_cast<Primitive*>(t);
if (primitive->primitiveType == P_CUBE)
{
Cube* cube = static_cast<Cube*>(primitive);
cube->Update();
}
//delete t;
}
if (go.type == TYPE_GAMEOBJECT)
{
}
The reason I have commented out "delete t;" is it breaks the compiler for some reason, when it really shouldn't as far as I can see. So currently it causes a small memory leak.
What is going on? All my update functions are writen with virtual void Update() so shouldnt it not matter what form the variable is in? be it a transform or a primitive, the function should call whatever code was applied to it in the first place?