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 ' ;}