C + + Asynchronous callbacks

Source: Internet
Author: User

In the previous article, we talked about the C language implementation of asynchronous callbacks through function pointers
This article continues the discussion of implementing callbacks in C + +, because there are classes in C + +, and the callback functions in C are not directly defined as member functions, so it is cumbersome to discuss the workaround below.
First know that the static member function is global, that is, the class, so presumably can use static member functions to implement the callback mechanism. Here's a little bit of knowledge about static members in a class.

#include <iostream>#include <stdio.h>#include <stdlib.h>using namespace STD;classxiabo_c{ Public: Xiabo_c (): A (Ten), D (8)    {printf("I am Xiabo_c () function\n"); }Static intSfunc ();//Static member functions also follow public,private,protected access rules    intFuncvoid); Public:intAStatic intb;//Cannot initialize here    Static Const intc =9;//Only static constants can be initialized directly    Const  intD;};intXiabo_c::b = One;the initialization of the//static member variable is only so that it cannot be initialized in constructionintXiabo_c::sfunc () {//!!! Static member functions when implemented outside the class, do not add static, do not write the static int Sfunc () {}, which is the inner and outer statically C function    //xiabo::a = 11; The error static member function cannot apply non-static membersXiabo_c::b = A;printf("I am static member Function,b =%d\n", xiabo_c::b);return 0;}Static intSfunc1 () {printf("I am static function, not member function \ n");return 0;}intXiabo_c::func (void) {xiabo_c::b = A;    Xiabo_c::sfunc (); Sfunc1 ();return 0;}intMainvoid) {Xiabo_c Xiabo;    Xiabo.func (); Xiabo_c::sfunc ();The //static member function is a class, not an object, and references must be accessed through the class nameGetChar ();return 0;}

The following is the use of static member functions to implement callbacks, the same as in the C language of the same structure, but changed to class

//-------------classxiabo2_c{ Public:typedef int(*PCB) (inta);typedef structparameter{intA    PCB callback;     }parameter; Xiabo2_c (): M_a (1){    }//Common functions    voidGetcallback (parameter* p)//write callback implemented by callback function{m_a =2;//do something         while(1)        {printf("Getcallback print! \ n "); _sleep ( -);        P->callback (P->a); }    }intSetcallbackfun (intA, PCB callback) {printf("Setcallbackfun print! \ n "); Parameter *p =Newparameter; P->a =Ten;        P->callback = callback; Getcallback (P);return 0; } Public:intM_a;};classxiabo2test_c{ Public: Xiabo2test_c (): M_b (1){    }Static intFcallback (intA//application-implemented callback function, static member function, but cannot access non-static members of the class, destroys the structure of the class{//do something        //m_b = A; The non-static members of the class cannot be accessed, the structure of the class is destroyed, and the application is cumbersome to use        printf("a =%d\n", a);printf("Fcallback print! \ n ");return 0; } Public:intM_b;};intMainvoid){//test_statichunc ();Xiabo2_c Xiabo2; Xiabo2. Setcallbackfun (5, Xiabo2test_c::fcallback); GetChar ();return 0;}


Although this method implements the callback, but the application is 10,000 reluctantly, in order to use a callback, my class class inside the non-static members of nothing can be used, or not callback it, let me directly call it. Is there a way to get the most out of both worlds? That's a must.

//-------------------Template<TypeNameTObject,TypeNameTparam>classxiabo3_c{typedef void(TObject::* cbfun) (tparam*); Public:BOOLExec (tparam* pparam);voidSet (tobject *pinstance,cbfun pfun,tparam* pparam);Private: Cbfun Pcbfun; Tobject* m_pinstance;};Template<TypeNameTObject,TypeNameTparam>voidXiabo3_c<tobject,tparam>::set (tobject *pinstance,cbfun pfun,tparam* pparam) {printf("Set print!\n");    M_pinstance = pinstance; (Pinstance->*pfun) (Pparam);//You can directly callback the function pointer passed here.Pcbfun = Pfun;}Template<TypeNameTObject,TypeNameTparam>BOOLXiabo3_c<tobject,tparam>::exec (tparam* pparam) {printf("Exec print!\n"); (M_pinstance->*pcbfun) (Pparam);//You can also recall the function pointer passed here.    return true;}classxiabo3test_c{ Public: Xiabo3test_c (): M_n ( -){    }voidFcallback (int*P) {printf("fcallback:sum = m_n + *p =%d\n", *p + m_n);printf("Fcallback print! I am a member function! I can access all the member, haha\n "); }Private:intM_n;};intMainvoid) {Xiabo3_c<xiabo3test_c,int> Xiabo3; Xiabo3test_c xiabo3test;intp = -; Xiabo3. Set (&AMP;XIABO3TEST,&AMP;XIABO3TEST_C::FCALLBACK,&AMP;P);//Xiabo3.    Exec (&AMP;P); GetChar ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Asynchronous callbacks

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.