函数指针与函数重载
成员函数与普通函数区别:
定义一个对象时,系统只为数据成员分配空间。那么对于类的成员函数而言,它如何知道函数中提到的数据成员是哪个对象的数据成员呢?……实际上,C++为每个成员函数设置了一个隐藏的指向本类型的指针形参this,它指向当前调用成员函数的对象。成员函数中对对象成员的访问时通过this指针实现的。……因此,当通过对象调用成员函数时,编译器会把相应对象的地址传给形参this。
1 /*重点:如果成员函数没有static,无法使用成员函数,因为存在this指针 2 由于静态成员函数没有this指针,使用可以使用函数指针!!! 3 */ 4 5 6 #include<iostream> 7 8 using namespace std; 9 10 class info { 11 public: 12 static void fun(int a); 13 static void fun(int a ,int b); 14 static void fun(int a, int b, int c); 15 private: 16 int age; 17 }; 18 19 20 void info::fun(int a) { 21 cout << a << endl; 22 } 23 24 void info::fun(int a, int b) { 25 cout << a << b << endl; 26 } 27 28 void info::fun(int a, int b, int c) { 29 cout << a << b << c << endl; 30 } 31 32 typedef void (pun)(int a, int b); 33 34 /* 函数指针定义三方法: 35 typedef void (pun)(int a, int b); 36 typedef void (*pun)(int a,int b); 37 void(*pun)(int a,int b); 38 */ 39 40 41 int main(void) 42 { 43 44 info info1; 45 46 pun * la = info1.fun; 47 48 la(3,4); 49 50 system("pause"); 51 52 return 0; 53 }
原文:https://www.cnblogs.com/panda-w/p/11356243.html