Jump to content
  • Advertisement
Sign in to follow this  
twistedjoe

[C++] Multiple Inheritance and polymorphism

This topic is 3084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to right my own c++ 2D game engine and am kinda stuck right now. My base engine class can play "state class" representing for example the menu state, a play state, etc..... At the beginning everything was simple, every Custom State class inheriting from my base State class. But now the design changed a bit and most of my custom State class inherit from a PhysicState class that inherit from the base State class so it was State -> CustomState and now its State -> PhysicState -> CustomState THEN i remember....you can't do polymorphism on 2 "layer" in C++ Is there any pattern to achieve this ??? Using Virtual inheritance or Interface ?

Share this post


Link to post
Share on other sites
Advertisement
1) You *can* have polymorphism over 2 layers like that.
2) That's not multiple inheritance. Multiple inheritance is when CustomState inherits from both PhysicState and State directly (instead of CustomState inheriting from PhysicState, which then inheritrs from State).

Share this post


Link to post
Share on other sites
I need to do something like this in the code somewhere

State* aState = new MyCustomState();

and it keep telling me that State is an unreachable parent of MyCustomState

Share this post


Link to post
Share on other sites
You've probably forgotten the word public when you specify your parent class ;)

e.g. MyCustomState : public PhysicsState
or PhysicsState : public State

Easy mistake to make, and the compiler error isn't very helpful unless you've come across this problem before.

Share this post


Link to post
Share on other sites
hum....when i test it.....polymorphism effectively work on more than one level, i was told not from somebody and I trusted him without testing.....

However.... I still have the problem even with the public keyword (it was already there). The design is a bit blurry right now, I will start by clean the mess then I'll see.

Thanks for the help ( I mean....I won't try a crazy hack to get around a non-existent problem now.....lol)

Share this post


Link to post
Share on other sites
Got it, I feel stupid right now, lol it was a namespace problem.....

I'll try to keep my code cleaner and don't trust to everything in the future :-P.

Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by twistedjoe
hum....when i test it.....polymorphism effectively work on more than one level, i was told not from somebody and I trusted him without testing.....


It's more likely that someone tried to tell you (and failed) that deep inheritance hierarchies are typically a sign of bad design. The conventional wisdom is to prefer composition (or aggregation, depending on you needs) over inheritance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!