Jump to content
  • Advertisement
Sign in to follow this  
Lode

OO and dynamic cast question

This topic is 3338 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 often have the habbit in C++ to make a class inherit from multiple abstract classes and using dynamic casts on incoming objects to check if they inherit from a certain interface or not. E.g. if an object inherits from an interface "IHelpTextProvider", some GUI or whatever will display a help text for that object, otherwise it'll treat it as an object that has no help text. Since C++ with its multiple inheritance is rare (Java has it but not initially and it's not "true" multiple inheritance, just interfaces), I have a qustion about this: Is this a good "pattern" from OO perspective or not? If not, what alternatives are there? If not, why not? What does the true OO philosophy have to say here?

Share this post


Link to post
Share on other sites
Advertisement
I think the way you're doing it is OK, but it's still vastly preferable to avoid it. I'm sure someone will come along with a way to refactor your example so you don't need it.

Share this post


Link to post
Share on other sites
I'm fine with the concept of multiple inheritance from interface classes, I see it used in C++ but the fact that C++ is a multi-paradigm language frequently reduces the need for this sort of thing also.

As for true OO, I suppose there is an element of whether or not you're violating the Single Responsibility Principle, one might argue that something inheriting from IHelpTextProvider has thereby been assigned its singlular responsibility and inheriting from anything further than that is probably just going to add more.

The use of dynamic casts is a bit funky though, if your function accepted an IHelpTextProvider then you wouldn't need to cast, or if you're using polymorphism then you could just inspect a getHelpText() function to remove the need to cast.

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!