Implementation of callback functions (pointer to a class member function)

Source: Internet
Author: User

See C and C ++CodeChunk Allison

 

Menu implementation
# Include <iostream> Using   Namespace  STD;  Class  Object {  Public  :  Void Retrieve () {cout < "  Object: retrieve \ n  "  ;}  Void Insert () {cout <"  Object: insert \ n  "  ;}  Void Update () {cout < "  Object: Update \ n  "  ;}  Void Process ( Int  Choice );  Private  :      Static   Void (Object: * farray [3  ]) () ;};  Void (Object: * object: farray [ 3 ]) () = { & Object: retrieve, & Object: insert, & Object: Update };  Void Object: Process ( Int  Choice ){  If ( 0 <= Choice & choice <=2  ){(  This -> * Farray [choice]) () ;}}  Int  Main (){  Int Show_menu ( Void  ); Object O;  For  (;;){  Int Choice = Show_menu (); cout < "  Choice = " <Choice < Endl;  If ( 1 <= Choice & choice <= 3  ) {O. Process (Choice - 1  );}  Else   If (Choice = 4  ){  Break  ;}} Return   0  ;}  Int Show_menu ( Void  ) {Cout < "  ======================== Menu ==============================  " < Endl; cout < "  1. Retrieve  " < Endl; cout <"  2. Insert  " < Endl; cout < "  3. Update  " < Endl; cout < "  4. Exit  " < Endl; cout < "  Please input Witch number you choose: "  ;  Char  Input; CIN > Input;  Return Input- '  0  '  ;} 
Running Effect

 

Use typedef to clear pointers to member functions
# Include <iostream> Using   Namespace  STD;  Class Object {  Public  :  Void Retrieve () {cout < "  Object: retrieve \ n  "  ;}  Void Insert () {cout < "  Object: insert \ n  "  ;}  Void Update () {cout < " Object: Update \ n  "  ;}  Void Process ( Int  Choice );  Private  : Typedef  Void (Object ::* OMF )();  Static OMF farray [ 3  ];  //  Static void (Object: * farray [3]) (); }; Object: OMF object: farray [  3 ] = //  Void (Object: * object: farray [3]) () =  { & Object: retrieve, & Object: insert, & Object: Update };  Void Object: Process ( Int  Choice ){  If ( 0 <= Choice & choice <=2  ){(  This -> * Farray [choice]) () ;}}  Int  Main (){  Int Show_menu ( Void  ); Object O;  For  (;;){  Int Choice = Show_menu (); cout < "  Choice = " <Choice < Endl;  If ( 1 <= Choice & choice <= 3  ) {O. Process (Choice - 1  );}  Else   If (Choice = 4  ){  Break  ;}} Return   0  ;}  Int Show_menu ( Void  ) {Cout < "  ======================== Menu ==============================  " < Endl; cout < "  1. Retrieve  " < Endl; cout <"  2. Insert  " < Endl; cout < "  3. Update  " < Endl; cout < "  4. Exit  " < Endl; cout < "  Please input Witch number you choose: "  ;  Char  Input; CIN > Input;  Return Input- '  0  '  ;} 

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.