Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

polymorphic_pointer_downcast example

#include <boost/polymorphic_pointer_cast.hpp>

class Fruit { public: virtual ~Fruit(){} };
class Banana : public Fruit {};

// use one of these:

typedef Fruit* FruitPtr;
typedef std::shared_ptr<Fruit> FruitPtr;
typedef boost::shared_ptr<Fruit> FruitPtr;
typedef boost::intrusive_ptr<Fruit> FruitPtr;

void f(FruitPtr fruit) {
  // ... logic which leads us to believe it is a banana
  auto banana = boost::polymorphic_pointer_downcast<Banana>(fruit);
  ...
}

PrevUpHomeNext