こうか。
#include <iostream> #include <algorithm> #include <vector> #include <boost/shared_ptr.hpp> #include <boost/bind.hpp> using namespace std; struct base { virtual void print() = 0; }; struct hoge : public base { virtual void print() { cout << "hoge" << endl; } }; struct hige : public base { virtual void print() { cout << "hige" << endl; } }; typedef boost::shared_ptr<base> basePtr; typedef boost::shared_ptr<hoge> hogePtr; typedef boost::shared_ptr<hige> higePtr; typedef vector<basePtr> baseArray; int main(int, char*[]) { baseArray arr; { hogePtr p(new hoge()); arr.push_back(p); } { higePtr p(new hige()); arr.push_back(p); } { hogePtr p(new hoge()); arr.push_back(p); } for_each(arr.begin(), arr.end(), boost::bind(&base::print, _1)); return 0; }
スマートポインタに包んじゃっても多態できるよ! というだけの話でした。はい。