This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Need help with dynamic_cast across shared libraries with gcc 3.2.3.
- From: Eljay Love-Jensen <eljay at adobe dot com>
- To: "Yuly Novikov" <novikov_yuly at hotmail dot com>, gcc at gcc dot gnu dot org, gcc-help at gcc dot gnu dot org
- Cc: novikov_yuly at hotmail dot com
- Date: Thu, 17 Jul 2003 10:00:21 -0500
- Subject: Re: Need help with dynamic_cast across shared libraries with gcc 3.2.3.
Hi Yuly,
>Unfortunately in the real code I use multiple inheritance + virtual inheritance, so reinterpret_cast will return wrong pointer. Any other ideas?
If you cannot follow the solution as per <http://gcc.gnu.org/faq.html#dso>, and you have multiple inheritance & virtual inheritance , and you cannot take EXTRAORDINARY measures such as re-implement your hierarchy using C structures (i.e, code the relationship explicitly "by hand") instead of the standard C++ mechanism (uff-da!) ... you're effed. And not in the good way.
Hmmm, one possible (and lame) solution is to do something like this:
class Derived;
class Base {
private:
Derived* mDerivedPtr; // see [1]
protected:
void setDerived(Derived* ptr) { mDerivedPtr = ptr; }
public:
Base() : mDerivedPtr(NULL) { }
Derived* getDerivedPtr() { return mDerivedPtr; }
};
class Derived : public Base {
public:
Derived() : Base() { setDerived(this); }
};
Then you could call...
Derived* ptr = myLocalBasePtr->getDerivedPtr()
...on the "other side of the fence" where you are currently doing a dynamic_cast downcast. Which returns a NULL if it isn't the expected type, similar to a dynamic_cast<*>. (If you prefer dynamic_cast<&>, like I do, you could simulate that as well.)
You'd have to instrument your Base class with these downcast methods for each-and-every derived class of interest. Which, granted, is lame. But that will allow fetching the correct pointer from the object, and doesn't rely on the RTTI stuff.
Good luck, HTH,
--Eljay