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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: void (T_Database::*type_a)()=0; 注意一下类方法的函数指针的定义方法。  点击(此处)折叠或打开 头文件 #include ...

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. }
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
存储 编译器 Linux
15 0
|
1天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
1天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
1天前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
|
3天前
|
C++
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
|
3天前
|
存储 编译器 C++
【C++成长记】C++入门 | 类和对象(中) |拷贝构造函数、赋值运算符重载、const成员函数、 取地址及const取地址操作符重载
【C++成长记】C++入门 | 类和对象(中) |拷贝构造函数、赋值运算符重载、const成员函数、 取地址及const取地址操作符重载
|
10天前
|
存储 C++
C++指针
C++指针
|
21天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
31 0
|
21天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
31 0
|
12天前
|
存储 算法 C语言
【C++初阶】8. STL初阶 + String类
【C++初阶】8. STL初阶 + String类
47 1