Re: C++: what could cause dynamic_cast<> to fail?
Re: C++: what could cause dynamic_cast<> to fail?
- Subject: Re: C++: what could cause dynamic_cast<> to fail?
- From: Jens Alfke <email@hidden>
- Date: Sun, 24 Jan 2010 09:04:25 -0800
On Jan 23, 2010, at 1:01 PM, Dan Caugherty wrote:
> (For the record, the function call logic is in a shared library, not that this should matter.)
It does matter, actually. Multiple binaries play havoc with RTTI unless you're careful. I ran into this a few years ago.
The problem is that you can end up with more than one copy of the 'value' class, if the class implementation is compiled into each binary. Ordinarily this won't matter because the two copies of the class are identical in their code, object layout and name. But RTTI identifies classes by pointer identity of their vtables, so the two 'value' classes won't show up as being the same for purposes of dynamic_cast.
{This is a situation I call a "Great Roe", after an old Woody Allen treatise on mythical beasts: "The Great Roe is a beast with the head of a lion, and the body of a lion, but not the same lion."}
The solution is to make sure that the source file defining the 'value' class ('value.cpp' or whatever) is only compiled into the shared library. That ensures there's only one copy of the vtable. Then you also have to make sure the library exports the symbols for 'value's vtable and methods, otherwise the other binaries will fail to link.
—Jens _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden