jackyluoyefeng 2010-01-26
C++作为一种语言,大多数的人都是把它默认为面向对象的支持,认为他就是C语言的替代品,一种延伸而已,而事际上,C++与C是完全不同的语言。用过C++语言的朋友都觉得C++语言的确是个好语言。
但有时由于特殊需要,不得不用C++,在C++中要想实现C#中的Thread等也是可行的,不过代码要稍复杂:
//delegate 0 #define DEFINE_DELEGATE(NAME, R)\ class NAME##Delegate\ {\ public:\ virtual ~NAME##Delegate(void){ }\ virtual R operator()(void) = 0;\ };\ template\ class NAME##DelegateImpl : public NAME##Delegate\ {\ private:\ typedef R (C::*F)(void);\ C* m_class;\ F m_fun;\ public:\ NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\ virtual R operator()(void)\ {\ if(m_class)\ return (m_class-*m_fun)();\ }\ };\ template\ NAME##Delegate* Make##NAME##Delegate(C* c, F f)\ {\ return new NAME##DelegateImpl(c, f);\ } ////////////////////////////////////////////////////////////////////////// //delegate 1 #define DEFINE_DELEGATE(NAME, R, P1)\ class NAME##Delegate\ {\ public:\ virtual ~NAME##Delegate(void){ }\ virtual R operator()(P1 p1) = 0;\ };\ template\ class NAME##DelegateImpl : public NAME##Delegate\ {\ private:\ typedef R (C::*F)(P1);\ C* m_class;\ F m_fun;\ public:\ NAME##DelegateImpl(C* c, F f){ m_class = c; m_fun = f; }\ virtual R operator()(P1 p1)\ {\ if(m_class)\ return (m_class-*m_fun)(p1);\ }\ };\ template\ NAME##Delegate* Make##NAME##Delegate(C* c, F f)\ {\ return new NAME##DelegateImpl(c, f);\ }