Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Sceadu

Dynamic casts bad everywhere?

This topic is 5734 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

It''s often said, that dynamic_casts are a sign of bad design, but I don''t really agree with this. Just a little example, I''m eager to hear your opinion about it: I''ve got a class Node in my Scenegraph, this class consists of several dynamic addable plugins which define the Node''s behavior (for instance PhysicsPlugin, to let the object be influenced by gravity etc.) The plugins the Node own are stored and managed in a vector by their base class Plugin. Now I want to access one of those, there is a function getPlugin(string) which returns a Plugin*. The class that calls this function now has to do a dynamic_cast to access its specific interface. Would you call this bad design or do you have any better solutions for managing such plugins without dynamic_cast ? Sceadu

Share this post


Link to post
Share on other sites
Advertisement
Yes, it's a bad design. The design of Plugin should be such that clients do not need to know the underlying type, usually accomplished through virtual function calls.

[edited by - Stoffel on December 6, 2002 4:04:55 PM]

Share this post


Link to post
Share on other sites
While both of the above poster are largely right ... it is not any worse than most plugin / extension architectures.

The key is that the NORMAL interface would be available without any form of dynamic casting ... so all plugins would automatically do something usefull without adding special case handlers ... but you still might have special case handlers which additionally check for each plugin to be of type "PhysicsPlugin" when applying physics adjustments, etc ...

as for dynamic_cast ... I use it, and primarily it is for situations like you mentioned, when there is a general purpose managed resource, but you want to do something special for extended versions of the class ...

I need to go through my code and find a good example .. because most trivial uses of dynamic_cast can be fixed by refactoring your class library (assuming of course that it is under your control) ... which is another use for it - maintenence programming.

Share this post


Link to post
Share on other sites
In fact it is so that every Plugin has also common interface (for example to update them every cycle). One of the reasons I used such a PluginSystem was to do something like dynamic inheritance, so it has some similarities with the Composite Pattern (for which I can''t find an other way round without casting).

Share this post


Link to post
Share on other sites
dynamic_cast seems like a c++ way to do QueryInterface, and in my opinion the plugin architecture may as well take advantage of it just like COM uses QI.

Share this post


Link to post
Share on other sites

  • 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!