#include #include #include #include #include struct Animal { Animal(std::string name) : name(name) {} std::string name; virtual ~Animal() = default; }; struct Cat : Animal { using Animal::Animal; }; struct Dog : Animal { using Animal::Animal; }; struct Bulldog : Dog { using Dog::Dog; }; BOOST_OPENMETHOD( poke, // method name (std::ostream&, virtual_ptr), // method signature void); // return type BOOST_OPENMETHOD_OVERRIDE( poke, // method name (std::ostream & os, virtual_ptr cat), // overrider signature void) { // return type os << cat->name << " hisses"; // overrider body } BOOST_OPENMETHOD_OVERRIDE(poke, (std::ostream & os, virtual_ptr dog), void) { os << dog->name << " barks"; } BOOST_OPENMETHOD_OVERRIDE( poke, (std::ostream & os, virtual_ptr dog), void) { next(os, dog); // call base overrider os << " and bites back"; } BOOST_OPENMETHOD_CLASSES(Animal, Cat, Dog, Bulldog); int main() { boost::openmethod::initialize(); //boost::openmethod::initialize(); std::unique_ptr felix(new Cat("Felix")); std::unique_ptr snoopy(new Dog("Snoopy")); std::unique_ptr hector(new Bulldog("Hector")); poke(std::cout, *felix); // Felix hisses std::cout << ".\n"; poke(std::cout, *snoopy); // Snoopy barks std::cout << ".\n"; poke(std::cout, *hector); // Hector barks and bites std::cout << ".\n"; }