C++ 类成员函数指针的使用方法
#include <iostream>void func(){ std::cout << "void func()" << std::endl; };class Test{public:Test() {}virtual ~Test() {}public:bool func1(int){ std::cout << "bool func_1(int)" << std::endl; return true; }virtual void func2(){ std::cout << "virtual void func_2()" << std::endl; }static void func3(bool){ std::cout << "static void func_3(bool)" << std::endl; }int m_value1 = 0;static int m_value2;};int Test::m_value2 = 1;int main(){Test test;// 普通函数指针void (*pfunc)() = &func;pfunc();// 普通成员函数指针bool (Test::*pfunc1)(int) = &Test::func1;(test.*pfunc1)(10);// 虚成员函数指针void (Test::*pfunc2)() = &Test::func2;(test.*pfunc2)();// 静态成员函数指针void (*pfunc3)(bool) = &Test::func3;pfunc3(true);// 普通成员变量指针int Test::*pvalue1 = &Test::m_value1;std::cout << "普通成员变量:" << test.*pvalue1 << std::endl;// 静态成员变量指针int *pvalue2 = &Test::m_value2;std::cout << "静态成员变量:" << *pvalue2 << std::endl;return 0;}
运行结果
如果觉得《C++ 类成员函数指针的使用方法》对你有帮助,请点赞、收藏,并留下你的观点哦!