Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

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