『Modern C++ Design』に載ってたネタ
ローカルクラスに対するポリモルフィズムができる
#include <iostream> #include <shand/shared_ptr.hpp> using namespace std; using namespace shand; class hoge { public: virtual void disp() const { cout << "hoge::disp" << endl; } // ローカルクラスのインスタンス取得 shared_ptr<hoge> local_info() const { class local : public hoge { virtual void disp() const { cout << "local::disp" << endl; } }; return shared_ptr<hoge>(new local); } }; int main() { hoge().local_info()->disp(); // local::disp return 0; }
いや〜、C++って楽しいな〜♪
モダン楽しいよ〜