The callback function is a very useful and important concept. When an event occurs, the system or other function automatically calls a function that you define. Callback functions are used in many situations in Windows programming, such as Hook callback functions: Mouseproc,getmsgproc and Enumwindows,drawstate callback functions, and many system-level callback procedures. In general, we use the callback function basically is in the C language style. This article describes a C + + Style callback object method. Implemented using template.
[CPP]View PlainCopy
- Template < class class, TypeName ReturnType, typename Parameter >
- Class Singularcallback
- {
- Public :
- typedef returntype (Class::* Method) (Parameter);
- Singularcallback (class* _class_instance, Method _method)
- {
- //Get object instance address, and call method address
- Class_instance = _class_instance;
- method = _method;
- };
- ReturnType operator () (Parameter Parameter)
- {
- //Call object method
- return (Class_instance->*method) (parameter);
- };
- ReturnType Execute (Parameter Parameter)
- {
- //Call object method
- return operator () (parameter);
- };
- Private:
- Class* class_instance;
- Method method;
- };
Example:
The following is a two class implementation.
[CPP]View PlainCopy
- Class A
- {
- Public :
- void output ()
- {
- Std::cout << "I am class A:D" << Std::endl;
- };
- };
- Class B
- {
- Public :
- BOOL MethodB (a A)
- {
- A.output ();
- return true;
- }
- };
Examples of various invocations of Singularcallback:
[CPP]View PlainCopy
- A;
- b b;
- singularcallback< B,bool,a >* CB;
- CB = New singularcallback< B,bool,a > (&B,&B::METHODB);
- if ((*CB) (a))
- {
- Std::cout << "CallBack fired successfully!" << Std::endl;
- }
- Else
- {
- Std::cout << "CallBack fired unsuccessfully!" << Std::endl;
- }
[CPP]View PlainCopy
- A;
- b b;
- singularcallback< B,bool,a >* CB;
- CB = New singularcallback< B,bool,a > (&B,&B::METHODB);
- if (Cb->execute (a))
- {
- Std::cout << "CallBack fired successfully!" << Std::endl;
- }
- Else
- {
- Std::cout << "CallBack fired unsuccessfully!" << Std::endl;
- }
[CPP]View PlainCopy
- A;
- b b;
- singularcallback< B,bool,a >CB (&B,&B::METHODB);
- if (CB (a))
- {
- Std::cout << "CallBack fired successfully!" << Std::endl;
- }
- Else
- {
- Std::cout << "CallBack fired unsuccessfully!" << Std::endl;
- }
[CPP]View PlainCopy
- Class AClass
- {
- Public :
- AClass (unsigned int _id): ID (_id) {};
- ~aclass () {};
- bool Amethod (std::string str)
- {
- Std::cout << "aclass[" << ID << "]:" << str << Std::endl;
- return true;
- };
- Private:
- unsigned int id;
- };
- typedef singularcallback < AClass, bool, std::string > Acallback;
- int main ()
- {
- Std::vector < acallback > callback_list;
- AClass A1 (1);
- AClass A2 (2);
- AClass A3 (3);
- Callback_list.push_back (Acallback (&A1, &aclass::amethod));
- Callback_list.push_back (Acallback (&A2, &aclass::amethod));
- Callback_list.push_back (Acallback (&A3, &aclass::amethod));
- For (unsigned int i = 0; i < callback_list.size (); i++)
- {
- Callback_list[i] ("abc");
- }
- For (unsigned int i = 0; i < callback_list.size (); i++)
- {
- Callback_list[i].execute ("abc");
- }
- return true;
- }
Reference:
C + + Callback solution
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. 4347329 such as the method of calling an unknown class object in the event-handling thread class, the thread object is only responsible for running the processing method when it is useful. You can download kylib:http://download.csdn.net/source/1538376 and use a lot of event method pointers in the Kylib class library, so you can refer to learning. Actually is the class method pointer, I think your method is not easy to use, although the principle is correct.
Can you take a look at my < how to use the member method pointers of a class? >: http://blog.csdn.net/kyee/archive/2009/03/20/4009735.aspx
C + + callback (CallBack) use method (in fact, is the class method pointer, I think your method is not easy to use, although the principle is correct)