关于C++类方法函数指针的定义和使用方法


void  ( T_Database : : * type_a ) ( ) = 0 ;
注意一下类方法的函数指针的定义方法。 


点击(此处)折叠或打开

  1. 头文件
  2. #include<iostream>
  3. using namespace std;
  4. class T_Database
  5. {
  6.         public:
  7.                 virtual void __Check() =0;
  8.                 virtual void __Rred() =0;
  9.                 virtual void __All() =0;
  10. };


  11. class T_Oracle:public T_Database
  12. {
  13.         public:
  14.                 virtual void __Check(){
  15.                         cout<<"Check ORA db"<<endl;
  16.                 }
  17.                 virtual void __Rred(){
  18.                         cout<<"Read ORA db"<<endl;
  19.                 }
  20.                 virtual void __All(){
  21.                         cout<<"Read and Check ORA db"<<endl;
  22.                 }
  23. };
  24. class T_Mysql:public T_Database
  25. {
  26.         public:
  27.                 virtual void __Check(){
  28.                         cout<<"Check MYSQL db"<<endl;
  29.                 }
  30.                 virtual void __Rred(){
  31.                         cout<<"Read MYSQL db"<<endl;
  32.                 }
  33.                 virtual void __All(){
  34.                         cout<<"Read and Check MYSQL db"<<endl;
  35.                 }
  36. };

  37. 点击(此处)折叠或打开

    1. 主调函数
    2. #include<iostream>
    3. using namespace std;
    4. #include"Class.h"

    5. int main(void)
    6. {
    7.         T_Database* db_a;
    8.         void (T_Database::*type_a)()=0;
    9.         int dbcho,typecho;
    10.         bool uinput=false;

    11.         while(uinput==false)
    12.         {
    13.                 cout<<"Input your db type[0]quit[1]Oracle[2]Mysql"<<endl;
    14.                 cin>>dbcho;
    15.                 switch(dbcho)
    16.                 {
    17.                         case 0:
    18.                                 {
    19.                                         uinput = true;
    20.                                         return 1;
    21.                                 }
    22.                         case 1:
    23.                                 {
    24.                                         db_a = new T_Oracle;
    25.                                         break;
    26.                                 }
    27.                         case 2:
    28.                                 {
    29.                                         db_a = new T_Mysql;
    30.                                         break;
    31.                                 }
    32.                         default:
    33.                                 {
    34.                                         cout<<"Please input 0-2"<<endl;
    35.                                         break;
    36.                                 }
    37.                 }
    38.                 if(dbcho!=1&&dbcho!=2)
    39.                 {
    40.                         continue;
    41.                 }

    42.                 cout<<"Input what do[0]quit[1]Check[2]Rred[3]All"<<endl;
    43.                 cin>>typecho;
    44.                 switch(typecho)
    45.                 {
    46.                         case 0:
    47.                                 {
    48.                                         cout<<"please input int"<<endl;
    49.                                         uinput = true;
    50.                                         return 2;
    51.                                 }
    52.                         case 1:
    53.                                 {
    54.                                         type_a = &T_Database::__Check;break;
    55.                                 }
    56.                         case 2:
    57.                                 {
    58.                                         type_a = &T_Database::__Rred;break;
    59.                                 }
    60.                         case 3:
    61.                                 {
    62.                                         type_a=&T_Database::__All;break;
    63.                                 }
    64.                         default:
    65.                         {
    66.                                 cout<<"Please input 0-3,will exit"<<endl;
    67.                                 return 3;
    68.                         }
    69.                 }
    70.                 (db_a->*type_a)();
    71.         }
    72.         return 0;
    73. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7728585/viewspace-2124050/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7728585/viewspace-2124050/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值