Jump to content
  • Advertisement

Archived

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

rileyriley

linking with libraries that use dynamic_cast

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

I have a library that uses dynamic_cast, and a program that uses that library but does not use dynamic_cast itself. In order to get them to link properly, I seem to have to enable runtime type info in BOTH. Is there a way to build this such that only the library using dynamic_cast needs to be linked with runtime type info? Thanks~

Share this post


Link to post
Share on other sites
Advertisement
In order for dynamic_cast to work, it needs some information in an object''s vtable. Now, if the library is going to use dynamic_cast on a pointer that you pass into it, then that object needs the RTTI info present. Hence, you need RTTI enabled in your program.

Also, and I may be wrong here, the type info may alter the layout of an object''s vtable, in which case anything you link together must have the same RTTI settings.

Share this post


Link to post
Share on other sites
oh, I see.. I was hoping that I could enable RTTI for only a few types - I have maybe 30 classes in this project and only use dynamic_cast on 3 of them, so it seems like a pretty big waste. Oh well, thanks for the info.

Share this post


Link to post
Share on other sites
Yeah, thats why people don''t really like RTTI, it doesn''t add much and makes everyone else alter their compile settings to match it.

Share this post


Link to post
Share on other sites
It''s also a sure sign that your inheritance hierarchy is broken...

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com

Share this post


Link to post
Share on other sites
quote:
Original post by dmikesell
It''s also a sure sign that your inheritance hierarchy is broken...



I have a GraphicsDevice interface, and a Drawable interface. I''m implementing both with DirectX, so I subclass each to D3D implementations.

The top level interface functions obviously use abstract types, but it does not make sense for the D3D implementations to accept abstract types, as they will only work with other D3D implementations. dynamic_cast allows me to check that the objects being passed are of the correct type while maintaining the top level abstract interfaces. I think this makes a lot of sense. Though one might argue that Java has already been written.. ;x

Share this post


Link to post
Share on other sites
Doesn''t fly in my book.

I''d make a RendererFactory that returns the appropriate derived classes based on a string you pass in. That way you have to try VERY hard to screw up.

Share this post


Link to post
Share on other sites
quote:
Original post by daerid
Ummm... I don''t see how enabling RTTI is a sign that your class hierarchy is broken.

It''s just a sign that blindly following (and parroting) back "rules" like these is foolish.

Share this post


Link to post
Share on other sites
The power of inheritance is the ability to work with abstractions. You lose that ability if you can't use your base classes generically. If your code contains stuff like this:


void process(GenericObject * obj)
{
if (obj->type() == TYPE_A) {
// process a type A object

} else if { obj->type() == TYPE B) {
// process a type B object

} ...


it can become brittle and hard to maintain. I'm sure it has some uses in rare circumstances, but all too often I've seen it used pretty much like the above example.

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com

[edited by - dmikesell on October 13, 2003 8:52:33 AM]

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!