C + + callback

Source: Internet
Author: User

C + + callbacks contain class function callbacks and non-class function callbacks.

Class function callback, the function pointer points to the function name, need to take the class scope, call to use the class pointer.

Like QT, define a function pointer with a return value of Qbytearray.

typedef qbytearray (Mythead::* func_construct) (Qbytearray line);//Defining function PointersQbytearray devicestatus_construct (Qbytearray);    Qbytearray realdata_construct (Qbytearray line);    Qbytearray dayrun_construct (Qbytearray);    Qbytearray monthcheck_construct (Qbytearray line);    Qbytearray flag_construct (Qbytearray line);    Qbytearray palarm_construct (Qbytearray line);    Qbytearray devicedefine_construct (Qbytearray line);    Qbytearray staff_construct (Qbytearray line);   Qbytearray Devicestatus_construct_str (QString line);  CaseE_devicestatus:pfunc= &mythead::d evicestatus_construct;  Break;  CaseE_realdata:pfunc= &mythead::realdata_construct;  Break;  CaseE_dayrun:pfunc= &mythead::d ayrun_construct;  Break;  CaseE_monthcheck:pfunc= &mythead::monthcheck_construct;  Break;  CaseE_flag:pfunc= &mythead::flag_construct;  Break;  CaseE_palarm:pfunc= &mythead::p alarm_construct;  Break;  CaseE_devicedefine:pfunc= &mythead::d evicedefine_construct;  Break;  CaseE_staff:pfunc= &mythead::staff_construct; callback: Qbytearray btline= ( This->*pfunc) (Strline.toutf8 ());

Non-class pointers are much simpler:

1typedefvoid(*func) (int,int )2 voidFunc1 (intNumaintNumB) {3printf (numa+NumB); 4 }5 6 voidFunc2 (intNumaintNumB) {7printf (NumA-NumB); 8 }9 Ten intMain () { One      intNumA =Ten; A     intnumb=5;  -Func PFunc = &func1; - PFunc (NumA, NumB); thePFunc = &Func2; - PFunc (numa,numb); - Return0; -}
View Code

C + + 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.