C + + callback function callback

Source: Internet
Author: User


Several ways to implement callback mechanism in C + +

(1) Callback mode
The essence of callback is to set a function pointer in and then invoke the method when it is necessary to trigger an event, such as Windows's window message handler function. For example code below, we need to trigger an event outside the notification when the download is complete:

(2) Sink Way
The essence of sink is that you follow each other's requirements to implement a C + + interface, and then set your implementation of the interface to the other side, the other side needs to trigger the event when the interface is called, the connection point in COM is in this way. The requirements for downloading files above, if implemented with sink, the code is as follows:

(3) The essence of Delegate mode delegate is to set member function pointers to each other and then let the other person call when the event needs to be triggered. C # in the way of delegate implementation of the event, so that C + + programmers are very envious, C + + because of the language itself, to achieve delegate is still very troublesome. The above example is implemented in the delegate way as follows:

  1. Class Cdownloaddelegatebase
  2. {
  3. Public
  4. virtual void Fire (const char* PURL, bool BOK) = 0;
  5. };
  6. Template<typename O, TypeName t>
  7. Class Cdownloaddelegate:public Cdownloaddelegatebase
  8. {
  9. typedef void (T::* Fun) (const char*, BOOL);
  10. Public
  11. Cdownloaddelegate (o* pObj = null, fun Pfun = null)
  12. : M_pfun (Pfun), M_pobj (POBJ)
  13. {
  14. }
  15. virtual void Fire (const char* PURL, BOOL BOK)
  16. {
  17. if (m_pfun! = NULL
  18. && m_pobj! = NULL)
  19. {
  20. (M_pobj->*m_pfun) (PURL, BOK);
  21. }
  22. }
  23. Private
  24. Fun M_pfun;
  25. o* M_pobj;
  26. };
  27. Template<typename O, TypeName t>
  28. cdownloaddelegate<o,t>* makedelegate (o* pobject, void (T::* pfun) (const char* PURL, BOOL))
  29. {
  30. return new Cdownloaddelegate<o, t> (Pobject, Pfun);
  31. }
  32. Class Cdownloadevent
  33. {
  34. Public
  35. ~cdownloadevent ()
  36. {
  37. Vector<cdownloaddelegatebase*>::iterator ITR = M_ardelegates.begin ();
  38. while (ITR! = M_ardelegates.end ())
  39. {
  40. Delete *itr;
  41. ++itr;
  42. }
  43. M_ardelegates.clear ();
  44. }
  45. void operator + = (cdownloaddelegatebase* p)
  46. {
  47. M_ardelegates.push_back (P);
  48. }
  49. void operator-= (cdownloaddelegatebase* p)
  50. {
  51. ITR ITR = Remove (M_ardelegates.begin (), M_ardelegates.end (), p);
  52. ITR itrtemp = ITR;
  53. while (itrtemp! = M_ardelegates.end ())
  54. {
  55. Delete *itr;
  56. ++itr;
  57. }
  58. M_ardelegates.erase (ITR, M_ardelegates.end ());
  59. }
  60. void operator () (const char* PURL, BOOL BOK)
  61. {
  62. ITR itrtemp = M_ardelegates.begin ();
  63. while (itrtemp! = M_ardelegates.end ())
  64. {
  65. (*itrtemp)->fire (PURL, BOK);
  66. ++itrtemp;
  67. }
  68. }
  69. Private
  70. Vector<cdownloaddelegatebase*> m_ardelegates;
  71. typedef vector<cdownloaddelegatebase*>::iterator ITR;
  72. };
  73. Class Cmydownloaderex
  74. {
  75. Public
  76. void DownloadFile (const char* PURL)
  77. {
  78. cout << "Downloading:" << pURL << "<< Endl;
  79. Downloadevent (PURL, true);
  80. }
  81. Cdownloadevent downloadevent;
  82. };
  83. Class Cmyfileex
  84. {
  85. Public
  86. void Download ()
  87. {
  88. Cmydownloaderex Downloader;
  89. Downloader.downloadevent + = Makedelegate (this, &cmyfileex::ondownloadfinished);
  90. Downloader. DownloadFile ("www.baidu.com");
  91. }
  92. virtual void ondownloadfinished (const char* PURL, BOOL BOK)
  93. {
  94. cout << "ondownloadfinished, URL:" << pURL << "status:" << BOK << Endl;
  95. }
  96. };

You can see the way the delegate code is much more than the other 2 ways above, and we are above the number of fixed parameters and types of implementation, if you want to implement variable parameters, it is more cumbersome. Variable parameters can be referenced in these 2 implementations:

Yet another C#-style Delegate Class in standard C + + Member Function pointers and the fastest Possible C + + Delegates

We can test our implementation with the following code:

Finally, a simple comparison of the above 3 implementation of the callback method:


The first method of callback is process-oriented, simple and flexible to use, just as the C language itself.
The second method of sink is object-oriented, used more in C + +, can encapsulate a set of callback interfaces in a sink, and is suitable for a series of more fixed callback events.
The third method of delegate is also object-oriented, and sink encapsulates a set of interfaces, delegate's encapsulation is in function units, granularity is smaller and more flexible than sink.

Which way do you prefer to implement callbacks?

C + + callback function callback

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.