#ifndef ONE_H #define ONE_H namespace one { struct Tst_2 { void f() { std::cout << "f2" << std::endl; } void g() { std::cout << "g2" << std::endl; } }; struct Tst_1 { void f() { std::cout << "f1" << std::endl; } void g() { std::cout << "g1" << std::endl; } }; struct Interface { template Interface(T* x) : x_(x), table_(vTable_::table) {} void f() { table_.f(x_); } void g() { table_.g(x_); } private: struct VTable { void (*f)(void*); void (*g)(void*); }; template struct vTable_ { static VTable table; static void f(void* x) { static_cast(x)->f(); } static void g(void* x) { static_cast(x)->g(); } }; private: VTable table_; void* x_; }; template Interface::VTable Interface::vTable_::table = {&Interface::vTable_::f, &Interface::vTable_::g}; void test() { Tst_2 t2; Interface i(&t2); i.f(); i.g(); Tst_1 t1; i = &t1; i.f(); i.g(); } } #endif