關於C++類方法函式指標的定義和使用方法

gaopengtttt發表於2016-08-25

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章