飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2442|回复: 1

[C/C++] c++ 类指针的使用例子,从Stack Overflow找到的

[复制链接]
  • TA的每日心情
    开心
    2023-2-7 08:49
  • 签到天数: 12 天

    [LV.3]偶尔看看II

    发表于 2018-12-7 23:05:46 | 显示全部楼层 |阅读模式
    c++关于类指针的使用,比较有帮助的例子说明,如下:
    [C++] 纯文本查看 复制代码
    //类函数 指针 (未单独编译)
    //参考链接:https://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer
    
    #include <iostream>
    #include <map>
    #include <vector>
    
    class test1 {
    public:
      test1() {}
      ~test1() {}
      void show() {
        int i = 0;
        std::cout << "i am show function" << std::endl;
      }
    };
    
    //高级应用
    class test2 {
    public:
      test2() {}
      ~test2() {}
      struct ff {
        void null() { std::cout << "i am class test2::ff function." << std::endl; }
      };
    
      typedef void (ff::*null)();
      operator null() {
        int i = 0;
        return &ff::null;
      }
      ff f1;
    };
    
    void fun1() { std::cout << "fun1" << std::endl; }
    
    int main() {
      test1 t1;
      void (test1::*p)() = NULL;
      p = &test1::show;
      (t1.*p)();
    
      test2 t2;
      void (test2::ff::*p1)() = NULL;
      p1 = t2;
      (t2.f1.*p1)();
    
      void (*p2)() = NULL;
      p2 = &fun1;
      (*p2)();
    
      return 0;

    PYG19周年生日快乐!
  • TA的每日心情
    无聊
    2019-1-25 12:46
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    发表于 2018-12-15 10:23:17 | 显示全部楼层
    挺好挺好,学习了
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表