Boost logo

Boost Users :

From: Olaf Petzold (yg-boost-users_at_[hidden])
Date: 2002-08-26 05:00:30


Hello,

I've trouble with the follwong snippet:

#include <iostream>
#include <boost/shared_ptr.hpp>

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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net