Re: Need help with dynamic_cast across shared libraries with gcc 3.2.3.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux