codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
class IEvent { public: virtual void onEvent() = 0; }; template<class T> class Object: public IEvent { typedef void (T::*Method)(); Method m; T *obj; public: Object(Method t) { obj = new T(); m = t; } ~Object() { delete obj; } void onEvent() { (obj->*m)(); } Object& operator=(Object &other) { obj = other.obj; other.obj = NULL; return *this; } }; #define EventObject(o, m) Object<o>(&o::m) // --------------------------------------- // 共通インタフェースを持っていない二人 class Foo { public: void onHoge() { printf("foo!\n"); } }; class Bar { public: void onUaaaaa() { printf("bar!\n"); } }; int main() { // もう一ひねり欲しい IEvent* arr[2]; arr[0] = new EventObject(Foo, onHoge); arr[1] = new EventObject(Bar, onUaaaaa); for(int i=0; i<2; ++i) arr[i]->onEvent(); delete arr[0]; delete arr[1]; return 0; }
Private
[
?
]
Run code
Submit