views:

352

answers:

2

Hi,

Let's say I have two classes, A and B, where B is a child class of A.

I also have the following function:

void foo(boost::shared_ptr<const A> a)
{
    boost::shared_ptr<const B> b = boost::dynamic_pointer_cast<const B>(a); // Error !
}

Compilation with gcc gives me the following errors:

C:\Boost\include/boost/smart_ptr/shared_ptr.hpp: In constructor 'boost::shared_ptr< <template-parameter-1-1> >::shared_ptr(const boost::shared_ptr<Y>&, boost::detail::dynamic_cast_tag) [with Y = const A, T = const B]':
C:\Boost\include/boost/smart_ptr/shared_ptr.hpp:522:   instantiated from 'boost::shared_ptr<X> boost::dynamic_pointer_cast(const boost::shared_ptr<U>&) [with T = const B, U = const A]'
src\a.cpp:10:   instantiated from here
C:\Boost\include/boost/smart_ptr/shared_ptr.hpp:259: error: cannot dynamic_cast 'r->boost::shared_ptr<const A>::px' (of type 'const class A* const') to type 'const class B*' (source type is not polymorphic)

What could possibly be wrong ?

Thank you.

EDIT

Actually, I found out how to avoid this, but I'm not sure to understand.

My A class was empty (and thus had no virtual destructor). If I add a virtual destructor, the error goes out. But I don't get it, why is this required ?

+7  A: 

dynamic_pointer_cast uses the C++ dynamic_cast internally and dynamic_cast requires your classes to have at least one virtual method. No virtual methods means no vtable and without vtable dynamic_cast would not be able to figure which casts are doable at run-time.

sbk
+1 This is mandated in 5.2.7/6 in the standard: "Otherwise, v shall be a pointer to or an lvalue of a polymorphic type (10.3)."
Andreas Brinck
@sbk So it could be the destructor, or any other method. It just needs to be virtual. Did I get this right ?
ereOn
Yes, virtual destructor is fairly common anyway for a base class.
Matthieu M.
+1  A: 

You can only use dynamic_cast on a Type* pointer if Type is a class with at least one virtual member function (virtual destructor counts). Since boost::dynamic_pointer_cast uses dynamic_cast internally the same restriction applies to it as well.

sharptooth