zoukankan      html  css  js  c++  java
  • 成员函数指针

    成员函数指针

     1 #include <iostream>
     2  using namespace std;
     3  
     4  // 非成员函数指针 调用方式 (* 指针变量名 ) ( 实参表 ) 
     5  // 成员函数指针 调用方式 (被调用函数的宿主对象. *(指针变量)).(实参表)
     6  // 成员函数指针
     7  class CMyClass
     8  {
     9  public:
    10      void __cdecl Say(void)
    11      {
    12          cout << __FUNCTION__ << endl;
    13      }
    14 
    15      // 成员指针 作为成员.
    16      void (__cdecl CMyClass:: *pfSay)(void);
    17  };
    18 
    19  // 成员指针 作为全局变量.
    20  void (__cdecl CMyClass:: *g_pfSay)(void);
    21 
    22  
    23 
    24  int _tmain(int argc, _TCHAR* argv[])
    25  {
    26      CMyClass cls;
    27 
    28      // 通过成员指针调用对象的成员函数的时候:  除了要有成员指针外,还要有合法对象或对象指针(被调函数的宿主对象).
    29 
    30 
    31      // 成员指针 作为成员.
    32      cls.pfSay = &CMyClass::Say;
    33      (cls.*(cls.pfSay))();           // or (lpCls->*(lpCls.pfSay))();
    34      // |    —————
    35      // |        |_________ 成员函数指针.
    36      // |
    37      // |__________________ 被调用函数的宿主对象.
    38 
    39      // 成员指针 作为全局变量.
    40      g_pfSay = &CMyClass::Say;
    41      (cls.*g_pfSay)();               // or (lpCls->*g_pfSay)();
    42      // |    —————
    43      // |     |_________ 成员函数指针.
    44      // |
    45      // |_______________ 被调用函数的宿主对象.
    46  
    47   return 0;
    48  }
  • 相关阅读:
    事物的五种配置方式(转载)
    Spring入门
    leetcode刷题3
    LeetCode刷题2
    LeetCode刷题1
    bootstraptable使用总结之前端样式设计
    初识Handsontable
    Java之file类
    OpenCV学习系列教程第五篇:测试和提高代码的效率
    OpenCV学习系列教程第四篇:图像的算术操作
  • 原文地址:https://www.cnblogs.com/happylong/p/1619672.html
Copyright © 2011-2022 走看看