[ create a new paste ] login | about

Link: http://codepad.org/I7AkKZoU    [ raw code | output | fork ]

C++, pasted on Feb 7:
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;
}


Output:
1
2
foo!
bar!


Create a new paste based on this one


Comments: