说实在的,我只是想写写巩固巩固而已,其实这方面的资料,别人已经分析的很详细啦。哈哈
俺只是把代码贴出来,知道怎么用而已,若真要知道其原理或推理过程,请参看下面的参考链接哦。
普通函数指针和类成员函数指针
举例:
#include "stdafx.h" #include <iostream> using namespace std; void getNameById(int iId){ cout << "chris";} void getAgeById(int iId){ cout << "30";} typedef void(*pFun1)(int); void main() { pFun1 pf = getNameById; pf(1); pf = getAgeById; pf(1); return; }
需要注意的是普通成员函数指针可以单独使用,而类成员函数指针不能单独使用,需要结合类对象使用。
举例:
#include "stdafx.h" #include <iostream> using namespace std; class CStudent { public: void getNameById(int iId){ cout << "chris";} void getAgeById(int iId){ cout << "30";} }; typedef void(CStudent::*pFun1)(int); void main() { CStudent student; <pre name="code" class="cpp"> pFun1 pf = &CStudent::getNameById; (student.*pf)(1); pf = &CStudent::getAgeById;; (student.*pf)(1); return; }
类成员函数的函数地址与类的地址的偏移量是固定不变的,其对象和对象的函数地址的偏移地址也是固定不变的,并且其两者相同。只要给出了类型对象,无论是指针和引用,都不会因为是否销毁而导致函数调用失败。但是有成员变量,这样就是玩火了。
举例:
#include "stdafx.h" #include <iostream> using namespace std; class CStudent { public: void getNameById(int iId){ cout << "chris";} void getAgeById(int iId){ cout << "30";} }; typedef void(CStudent::*pFun1)(int); void main() { pFun1 pf = &CStudent::getNameById; CStudent *pstudent = new CStudent; (pstudent->*pf)(1); pf = &CStudent::getAgeById;; (pstudent->*pf)(1); delete pstudent; pstudent = NULL; pstudent->getNameById(1); pstudent->getAgeById(1); return; }
在delete pstudent指针后,pstudent任然可以成功调用getNameById和getAgeById。
举例:
#include "stdafx.h" #include <iostream> using namespace std; class CStudent { public: CStudent(LPSTR name) {m_name = name;} void getNameById(int iId){ cout << m_name;} void getAgeById(int iId){ cout << "30";} private: LPSTR m_name; }; typedef void(CStudent::*pFun1)(int); void main() { pFun1 pf = &CStudent::getNameById; LPSTR pName = "chris"; CStudent *pstudent = new CStudent(pName); (pstudent->*pf)(1); pf = &CStudent::getAgeById;; (pstudent->*pf)(1); delete pstudent; pstudent = NULL; pstudent->getAgeById(1); <pre name="code" class="cpp"> pstudent->getNameById(1); return; }
在delete pstudent指针后,pstudent调用getNameById会引起崩溃。因为delete之后,其成员变量已析构,变量的地址在空,当再次访问空地址的变量时,就会引起访问违规(0xC0000005: Access violation reading location 0x00000000.),但是访问getAgeById依然可以正常访问哦。
是不是有点感觉啦,哈哈。
写写blog,真好,可以让自己加深理解,同时还可以作为备份,真地感觉很棒啊。
【参考】
http://blog.csdn.net/hairetz/article/details/4153252
http://blog.csdn.net/eroswang/article/details/4153356