dynamic_pointer_cast doesn't work as expected

Hi, see these lines: boost::shared_ptr<Executor> executor(&(createExecutor(subclass,name))); if(!executor) { throw LoadingClassException("createExecutor function of\""+subclass+"\" return null."); } //executor points to an object of class ExecDebugger that IS A Executor //(it's a plugin loaded at run time via Qt) boost::shared_ptr<Nameable> a=executor; boost::shared_ptr<Executor> sp; sp=boost::dynamic_pointer_cast<Executor>(a); --------------------------------------------------------------------------------------------------------------------- a is a Nameable, it points to a ExecDebugger object. ExecDebugger IS A Executor, Executor IS A Nameable...but sp is always ==0! Why?! Thanks, Diego Bernini

Diego Bernini wrote:
see these lines:
boost::shared_ptr<Executor> executor(&(createExecutor(subclass,name))); if(!executor) { throw LoadingClassException("createExecutor function of\""+subclass+"\" return null."); } //executor points to an object of class ExecDebugger that IS A Executor //(it's a plugin loaded at run time via Qt)
boost::shared_ptr<Nameable> a=executor; boost::shared_ptr<Executor> sp;
sp=boost::dynamic_pointer_cast<Executor>(a);
Please post a minimal complete example we could compile? I must confess I don't quite understand the class hierarchy you have in mind. I also don't understand the first line. If the return type of createExecutor() is Executor*, why are you taking the address of the returned value with & ? Wouldn't that produce an Executor**? But if the return type of createExecutor() is Executor, aren't you taking the address of a temporary object that will be destroyed?

Diego Bernini:
Hi, see these lines:
boost::shared_ptr<Executor> executor(&(createExecutor(subclass,name))); if(!executor) { throw LoadingClassException("createExecutor function of\""+subclass+"\" return null."); } //executor points to an object of class ExecDebugger that IS A Executor //(it's a plugin loaded at run time via Qt)
boost::shared_ptr<Nameable> a=executor; boost::shared_ptr<Executor> sp;
sp=boost::dynamic_pointer_cast<Executor>(a);
It's possible that dynamic_cast doesn't work across dynamic libraries on your platform.

Peter Dimov wrote:
Diego Bernini:
sp=boost::dynamic_pointer_cast<Executor>(a);
It's possible that dynamic_cast doesn't work across dynamic libraries on your platform.
Ah. You wouldn't be using OS X 10.3.9, would you? I've definitely bumped into dynamic_cast failures (note, ordinary builtin dynamic_cast, never mind the Boost dynamic_pointer_cast wrapper!) across libraries on 10.3.9.

At 11:37 PM +0300 9/28/07, Peter Dimov wrote:
Diego Bernini:
Hi, see these lines:
boost::shared_ptr<Executor> executor(&(createExecutor(subclass,name))); if(!executor) { throw LoadingClassException("createExecutor function of\""+subclass+"\" return null."); } //executor points to an object of class ExecDebugger that IS A Executor //(it's a plugin loaded at run time via Qt)
boost::shared_ptr<Nameable> a=executor; boost::shared_ptr<Executor> sp;
sp=boost::dynamic_pointer_cast<Executor>(a);
It's possible that dynamic_cast doesn't work across dynamic libraries on your platform.
See http://lists.apple.com/archives/xcode-users/2006/Jul/msg00301.html (start there) and http://lists.apple.com/archives/xcode-users/2006/Jul/msg00386.html and http://developer.apple.com/technotes/tn2007/tn2185.html for a description of this problem (on Mac OS X, but really a gcc problem) - especially the part around "Comparing types is important for catching exceptions, dynamic_cast, and comparing type_info's obtained by a typeid expression." -- -- Marshall Marshall Clow Idio Software mailto:marshall@idio.com It is by caffeine alone I set my mind in motion. It is by the beans of Java that thoughts acquire speed, the hands acquire shaking, the shaking becomes a warning. It is by caffeine alone I set my mind in motion.
participants (4)
-
Diego Bernini
-
Marshall Clow
-
Nat Goodspeed
-
Peter Dimov