Hello,
I've trouble with the follwong snippet:
#include <iostream>
#include
struct A {
A(const char* n) : name(n) { }
const char* name;
};
struct B : public A {
B(const char* n) : A(n) { std::cout << "construct " << name <<
std::endl; }
~B() { std::cout << "destruct " << name << std::endl; }
};
struct BaseC {
virtual void set(const boost::shared_ptr<A>& ptr) = 0;
};
struct C : public BaseC {
C() : b( new B("C::b") ) { }
void set(const boost::shared_ptr<A>& ptr) {
b.swap( boost::shared_static_cast<B>( ptr ) ); // L21
}
void print() { std::cout << b->name << std::endl; }
boost::shared_ptr<B> b;
};
using namespace std;
int main() {
boost::shared_ptr<A> b( new B("b") );
C c;
c.print();
c.set( b );
c.print();
}
t.cc: In member function `virtual void C::set(const boost::shared_ptr<A>&)':
t.cc:21: no matching function for call to
`boost::shared_ptr<B>::swap(boost::shared_ptr<B>)'
/usr/include/boost/shared_ptr.hpp:234: candidates are: void
boost::shared_ptr<T>::swap(boost::shared_ptr<T>&) [with T = B]
I did try boost::shared_polymorphic_cast to get a std::bad_cast exception -
but it doesn't compile too. How can get it working?
Thanks
Olaf