二. 函数指针类型为类成员函数. 以下是引用片段: #include "stdafx.h" #include using namespace std; class Action; class TestAction; // 函数指针类型为类 Action 的成员函数 typedef void (Action::*fp)(int); class Action { public: void Drink(int i) { cout<<"No. "< } void Eat(int i) { // 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=1005&d=sc37og cout<<"No. "< } }; class TestAction { public: // 定义一个函数指针 fp testAct; //Action 对象实例 , 该指针用于记录被实例化的 Action 对象 Action * pAction; void TestAct(int i) { if ((pAction != NULL) && (testAct != NULL)) { // 调用 (pAction->*testAct)(i); } } }; int main(int argc, char* argv[]) { Action act; TestAction doact; doact.pAction = &act; doact.testAct = Action::Drink; doact.TestAct(0); doact.TestAct(1); doact.TestAct(2); doact.testAct = Action::Eat; doact.TestAct(0); doact.TestAct(1); doact.TestAct(2); return 0; } |
 
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
2/2 首页 上一页 1 2 |