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)

Source: Internet
Author: User
Tags thread class

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
  1. Template < class class, TypeName ReturnType, typename Parameter >
  2. Class Singularcallback
  3. {
  4. Public :
  5. typedef returntype (Class::* Method) (Parameter);
  6. Singularcallback (class* _class_instance, Method _method)
  7. {
  8. //Get object instance address, and call method address
  9. Class_instance = _class_instance;
  10. method = _method;
  11. };
  12. ReturnType operator () (Parameter Parameter)
  13. {
  14. //Call object method
  15. return (Class_instance->*method) (parameter);
  16. };
  17. ReturnType Execute (Parameter Parameter)
  18. {
  19. //Call object method
  20. return operator () (parameter);
  21. };
  22. Private:
  23. Class* class_instance;
  24. Method method;
  25. };

Example:

The following is a two class implementation.

[CPP]View PlainCopy
  1. Class A
  2. {
  3. Public :
  4. void output ()
  5. {
  6. Std::cout << "I am class A:D" << Std::endl;
  7. };
  8. };
  9. Class B
  10. {
  11. Public :
  12. BOOL MethodB (a A)
  13. {
  14. A.output ();
  15. return true;
  16. }
  17. };

Examples of various invocations of Singularcallback:

[CPP]View PlainCopy
  1. A;
  2. b b;
  3. singularcallback< B,bool,a >* CB;
  4. CB = New singularcallback< B,bool,a > (&B,&B::METHODB);
  5. if ((*CB) (a))
  6. {
  7. Std::cout << "CallBack fired successfully!" << Std::endl;
  8. }
  9. Else
  10. {
  11. Std::cout << "CallBack fired unsuccessfully!" << Std::endl;
  12. }

[CPP]View PlainCopy
  1. A;
  2. b b;
  3. singularcallback< B,bool,a >* CB;
  4. CB = New singularcallback< B,bool,a > (&B,&B::METHODB);
  5. if (Cb->execute (a))
  6. {
  7. Std::cout << "CallBack fired successfully!" << Std::endl;
  8. }
  9. Else
  10. {
  11. Std::cout << "CallBack fired unsuccessfully!" << Std::endl;
  12. }

[CPP]View PlainCopy
    1. A;
    2. b b;
    3. singularcallback< B,bool,a >CB (&B,&B::METHODB);
    4. if (CB (a))
    5. {
    6. Std::cout << "CallBack fired successfully!" << Std::endl;
    7. }
    8. Else
    9. {
    10. Std::cout << "CallBack fired unsuccessfully!" << Std::endl;
    11. }

[CPP]View PlainCopy
  1. Class AClass
  2. {
  3. Public :
  4. AClass (unsigned int _id): ID (_id) {};
  5. ~aclass () {};
  6. bool Amethod (std::string str)
  7. {
  8. Std::cout << "aclass[" << ID << "]:" << str << Std::endl;
  9. return true;
  10. };
  11. Private:
  12. unsigned int id;
  13. };
  14. typedef singularcallback < AClass, bool, std::string > Acallback;
  15. int main ()
  16. {
  17. Std::vector < acallback > callback_list;
  18. AClass A1 (1);
  19. AClass A2 (2);
  20. AClass A3 (3);
  21. Callback_list.push_back (Acallback (&A1, &aclass::amethod));
  22. Callback_list.push_back (Acallback (&A2, &aclass::amethod));
  23. Callback_list.push_back (Acallback (&A3, &aclass::amethod));
  24. For (unsigned int i = 0; i < callback_list.size (); i++)
  25. {
  26. Callback_list[i] ("abc");
  27. }
  28. For (unsigned int i = 0; i < callback_list.size (); i++)
  29. {
  30. Callback_list[i].execute ("abc");
  31. }
  32. return true;
  33. }

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 &lt; how to use the member method pointers of a class? &gt;: 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.