void ( T_Database : : * type_a ) ( ) = 0 ;
注意一下类方法的函数指针的定义方法。
点击(此处)折叠或打开
- 头文件
- #include<iostream>
- using namespace std;
- class T_Database
- {
- public:
- virtual void __Check() =0;
- virtual void __Rred() =0;
- virtual void __All() =0;
- };
-
-
- class T_Oracle:public T_Database
- {
- public:
- virtual void __Check(){
- cout<<"Check ORA db"<<endl;
- }
- virtual void __Rred(){
- cout<<"Read ORA db"<<endl;
- }
- virtual void __All(){
- cout<<"Read and Check ORA db"<<endl;
- }
- };
- class T_Mysql:public T_Database
- {
- public:
- virtual void __Check(){
- cout<<"Check MYSQL db"<<endl;
- }
- virtual void __Rred(){
- cout<<"Read MYSQL db"<<endl;
- }
- virtual void __All(){
- cout<<"Read and Check MYSQL db"<<endl;
- }
- };
-
点击(此处)折叠或打开
- 主调函数
- #include<iostream>
- using namespace std;
- #include"Class.h"
-
- int main(void)
- {
- T_Database* db_a;
- void (T_Database::*type_a)()=0;
- int dbcho,typecho;
- bool uinput=false;
-
- while(uinput==false)
- {
- cout<<"Input your db type[0]quit[1]Oracle[2]Mysql"<<endl;
- cin>>dbcho;
- switch(dbcho)
- {
- case 0:
- {
- uinput = true;
- return 1;
- }
- case 1:
- {
- db_a = new T_Oracle;
- break;
- }
- case 2:
- {
- db_a = new T_Mysql;
- break;
- }
- default:
- {
- cout<<"Please input 0-2"<<endl;
- break;
- }
- }
- if(dbcho!=1&&dbcho!=2)
- {
- continue;
- }
-
- cout<<"Input what do[0]quit[1]Check[2]Rred[3]All"<<endl;
- cin>>typecho;
- switch(typecho)
- {
- case 0:
- {
- cout<<"please input int"<<endl;
- uinput = true;
- return 2;
- }
- case 1:
- {
- type_a = &T_Database::__Check;break;
- }
- case 2:
- {
- type_a = &T_Database::__Rred;break;
- }
- case 3:
- {
- type_a=&T_Database::__All;break;
- }
- default:
- {
- cout<<"Please input 0-3,will exit"<<endl;
- return 3;
- }
- }
- (db_a->*type_a)();
- }
- return 0;
- }
- 主调函数