#include <iostream> using namespace std; class Student{ public: Student(const string& name):m_name(name){} void who(void){ cout << m_name << endl; } string m_name; static int s_add(int a,int b){ return a+b; } }; int main(void){ //成员函数指针 void (Student::*pwho)(void) = &Student::who; Student s1("zhnangfei"); Student s2("zhaoyun"); (s1.*pwho)(); Student* ps = &s2; (ps->*pwho)(); //指向静态成员函数的指针 int (*padd)(int,int) = Student::s_add; //int (Student::*padd1)(int,int) = Student::s_add;错误的写法 cout << padd(100,200) << endl; return 0; } 成员函数的指针(即函数指针): 类型 ()(形参类型) = &类名::成员函数名(注意类名前一定要加取地址符&,不然报错) 指向静态成员函数的指针(还是函数指针): 类型 ()(形参类型) = Student::s_add(注意类型前面可以不加&也可以加)
时间: 2024-10-07 00:23:43