C++指向成员的指针

一、指向成员变量的指针并非指针

         C++中指向成员变量的指针其实是一个相对于类对象的偏移量。《C++必知必会》的条款15讲述了这一说法:“与常规指针不同,一个指向成员变量的指针并不指向一个内存位置。它指向的是一个类的特定成员,而不是指向一个特定对象里的特定成员。通常最清晰的做法是将指向数据成员的指针看作为一个偏移量。......。这个偏移量告诉你,一个特定成员的位置距离对象的起点有多少个字节。”

 

二、指向成员函数的指针并非指针

      获取非静态成员函数的地址时,得到的不是一个地址,而是一个指向成员函数的指针。为了对一个指向成员函数的指针进行解引用,需要一个对象或一个指向对象的指针。因为通过指向成员函数的指针调用该函数时,需要将对象的地址用作this指针的值,以便进行函数调用(当然,也有其它的用途)。

 

以下内容转自: http://blog.csdn.net/maojudong/article/details/8194143

测试目录:

1.普通函数指针指向普通函数
2.普通函数指向非静态成员函数
3. 类外部的 类函数指针 指向普通函数
4. 类外部的 类函数指针 指向成员函数
5. 类内部的 函数指针 指向成员函数 (类似于第2条)
6. 类内部的 函数指针 指向普通函数

 
直接上代码:
[cpp] view plain copy
 
 print?
  1. #include <iostream>  
  2. #include <string>  
  3. #include <vector>  
  4. #include <map>  
  5. using namespace std;  
  6.    
  7. class Foo  
  8. {  
  9.     public:  
  10.         string m_str;  
  11.         Foo()  
  12.         {  
  13.             m_str = "";  
  14.         }  
  15.           
  16.         static void testFunc2(int a)  
  17.         {  
  18.             cout<<"Foo:void testFunc2(int a)"<<endl;  
  19.             cout<<a<<endl;  
  20.         }  
  21.           
  22.         void testFunc4(int a)  
  23.         {  
  24.             cout<<"Foo:void testFunc4(int a)"<<endl;  
  25.             cout<<a<<endl;  
  26.         }  
  27.         static void testFunc5(int a)  
  28.         {  
  29.             cout<<"Foo:void testFunc5(int a)"<<endl;  
  30.             cout<<a<<endl;  
  31.         }  
  32.           
  33.         void (*pTestFunc5)(int a);  
  34.         void (*pTestFunc6)(int a);  
  35. };  
  36.   
  37. void (*pTestFunc1)(int a);  
  38. void (*pTestFunc2)(int a);  
  39. void (Foo::*pTestFunc3)(int a);  
  40. void (Foo::*pTestFunc4)(int a);  
  41.   
  42. void testFunc1(int a)  
  43. {  
  44.     cout<<"func1 pointer test"<<endl;  
  45.     cout<<a<<endl;  
  46. }  
  47.   
  48. void testFunc3(int a)  
  49. {  
  50.     cout<<"func3 pointer test"<<endl;  
  51.     cout<<a<<endl;  
  52. }  
  53.   
  54. void testFunc6(int a)  
  55. {  
  56.     cout<<"func6 pointer test"<<endl;  
  57.     cout<<a<<endl;  
  58. }  
  59.   
  60.   
  61. int main(int argc, const char *argv[])  
  62. {  
  63.   
  64.     Foo foo;  
  65.     //foo.test("woo",100);  
  66.       
  67.     pTestFunc1 = testFunc1;     //经常用这个方法  
  68.     (*pTestFunc1)(1);  
  69.       
  70.      pTestFunc2=&foo.testFunc2;  
  71.     (*pTestFunc2)(2);  
  72.       
  73.     //pTestFunc3 = &testFunc3;  //编译器报错,不可以这么使用  
  74.       
  75.     pTestFunc4 = &Foo::testFunc4; //初始化的时候必须带有&Foo::  
  76.   
  77.     //pTestFunc4(4);//编译器报错,不可以这么使用  
  78.     //foo.pTestFunc4(4);//编译器报错,不可以这么使用  
  79.     //foo.*pTestFunc4(4);//编译器报错,不可以这么使用  
  80.     //foo.(*pTestFunc4)(4);//编译器报错,不可以这么使用  
  81.     (foo.*pTestFunc4)(4);   //正常运行  
  82.       
  83.     foo.pTestFunc5=&Foo::testFunc5;  
  84.     foo.pTestFunc5(5);  
  85.       
  86.     foo.pTestFunc6=&testFunc6;  
  87.     foo.pTestFunc6(6);  
  88.       
  89.     return 0;  
  90. }  

 

posted @ 2016-07-13 10:49  Andy.gbhu  阅读(314)  评论(0编辑  收藏  举报