Skip to main content
gamedev.net
🔒 Locked

Inheritance access problem

Started by Shanee Sep 9, 2010 at 11:15 PM 2 replies 550+ views
Original Post
Shanee
Shanee
I have this class called cEntity3D which has a WorldPosition class in it.

I made an inherited class called cActor3D

I also have an WorldManager that has a hash map of DWORD/cEntity3D (ID and entity) which can have entities added to it.

The next few lines of code work just fine:

cActor3D* actor = new cActor3D();actor->SetModel("Content/char.X");actor->GetWorldPosition()->MoveY(100);WorldManager->AddEntity(actor, false);


But:
WorldManager->GetEntity(0)->GetWorldPosition()->MoveX(-speed * GlobalTimer->GetTimeFromLastFrameMicroF());


Does not work at all if the retrieved Entity (in this case it is the same actor we created above) is an cActor3D. Also updates inside the WorldManager and collision tests don't seem to work properly when the entity retrieved is inherited and not the original cEntity3D class.

Any suggestions?
Zipster
Zipster
Define "does not work". The position isn't updated at all, it updates to an incorrect value, the wrong entity altogether is updated, your application crashes, gnomes appear from the air vents and start kicking you in the spleen, etc. Have you stepped through with the debugger and checked what's happening? At first glance there doesn't appear to be anything wrong with your code, which means that something is going wrong where we can't see.
Shanee
Shanee
The position didn't update, for some reason changing the value from protected to private fixed it, not sure why.
rip-off
rip-off
It might have triggered a build. Due to its complicated and old-fashioned compilation model, even modern build tools can sometimes get themselves confused about what should be rebuilt. In some ways it might be worse for modern tools as they try hardest to minimise excessive building.

As a precaution, if you ever come across a "What the hell" situation in C++, it can be a good idea to do a clean build.

Aside: your code severely breaks the "law of demeter". Reaching deep inside an object like that is often a sign of a brittle design, with insufficient encapsulation.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.