C ++, as a language, most people regard it as object-oriented support by default, and think it is an extension of C language, C ++ and C are completely different languages. All the friends who have used the C ++ language think that the C ++ language is indeed a good language.
But sometimes, due to special needs, C ++ has to be used. It is also feasible to implement Thread in C # In C ++, but the code should be slightly complicated:
- //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);\
- }
This example is relatively simple. The C ++ language is used to implement a function. The client is a client program. There are several methods to send notifications to the client when m_sdk is used for processing. One is sending thread messages, and the other is callback, but the traditional callback does not support object-oriented, which is the starting point for implementing delegate. The following is an implementation: class Delegate
- DEFINE_DELEGATE (Open, void, int, string)
- Class sdk
- {
- Public:
- OpenDelegate * pEvent;
- Sdk (): pEvent (NULL ){}
- Void Open ()
- {
- Cout
- If (pEvent! = NULL)
- (* PEvent) (100, "hello ");
- }
- };
- Class client
- {
- Private:
- Sdk m_sdk;
- Public:
- Client ()
- {
- M_sdk.pEvent=MakeOpenDelegate(This, OnOpen2 );
- }
- Void Open ()
- {
- M_sdk.Open ();
- }
- Void OnOpen ()
- {
- Cout
- }
- Void OnOpen2 (int t, string str)
- {
- Cout
- }
- };
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code