linking with libraries that use dynamic_cast

Started by
14 comments, last by rileyriley 20 years, 6 months ago
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~
--Riley
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.
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.
--Riley
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
It''s also a sure sign that your inheritance hierarchy is broken...

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com
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

--Riley
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Ummm... I don''t see how enabling RTTI is a sign that your class hierarchy is broken.
daerid@gmail.com
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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]

This topic is closed to new replies.

Advertisement