Need help with dynamic_cast across shared libraries with gcc 3.2.3.
Eljay Love-Jensen
eljay@adobe.com
Thu Jul 17 15:06:00 GMT 2003
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
More information about the Gcc-help
mailing list