Pointer to class member function (turn)

Original: https://blog.csdn.net/jinjinClouded/article/details/5189540?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu- 2

 

      I recently used function pointers in development, so I wanted to sort out the concept of function pointers. O (∩_∩) O ~

     First of all, the function pointer is a pointer to a group of functions of the same type; and the class member function can be similarly considered to be a pointer to a member function of the same type in the same class. Of course, the member function here should be more accurately Refers to non-static member functions. The former directly points to the function address, while the latter we can also literally know that it must be related to the class and the object.

     Function pointer example:

typedef int (* p) (int, int); // Define a function pointer type that accepts two int types and returns int variables 
int func (int x, int y) 
{ 
 printf ("func: x =% d, y =% d / n ", x, y); 
 return (x <y? x: y); 
} 

int main () 
{ 
 p fun = func; // define a function pointer and assign it a function pointer 
 cout < <"min:" << (* fun) (4,5) << endl; // Why does * fun need to be expanded with ()? Because the operator of * has a lower priority than (), if it is not used () It becomes * (fun ()) 
 return 0; 
} 

   and the "pointer to the class member function" has one more class difference: 

class A 
{ 
public: 
 int func (int x, int y) 
 { 
  printf ("A :: func: x =% d, y =% d / n ", x, y); 
  return (x <y? x: y); 
 } 
}; 
typedef int (A :: * p) (int, int); / / Pointer name must be added before the qualified type class name A :: qualified 

int main () 
{ 
 p fun = & A :: func;
 A a; // Because the dereference of the address of the member function must be attached to the address of an object, we must create an object. 
 cout << "min:" << (a. * fun) (4,5) << endl; 
 return 0; 
}

 

Hey. . Just use it. * It feels weird.

 

Next, we can expand the following:

 

#include <tchar.h>
#include <iostream>
#include <stdio.h>
using namespace std;


class A
{
public:
 int func1(int x,int y)
 {
  printf("A::func:x=%d,y=%d/n",x,y);
  return (x<y?x:y);
 }
 virtual int func2(int x,int y)
 {
  printf("A::func:x=%d,y=%d/n",x,y);
  return (x>y?x:y);
 }
};
class B:public A
{
public:
 virtual int func2(int x,int y)
 {
  printf("B::func:x=%d,y=%d/n",x,y);
  return (x+y);
 }

};
typedef int (A::*p)(int,int);//指针名前一定要加上所属类型类名 A::的限定
typedef int (B :: * p0) (int, int);

int main()
{
 A a; // Because the dereference of the address of the member function must be attached to the address of an object, we must create an object. 
 B b; 
 p fun = & A :: func1; 

 cout << (a. * Fun) (4,5) << endl; 
 cout << (b. * Fun) (4,5) << endl << endl; 

 fun = & A :: func2; 
 cout << (a. * fun) (4,5) << endl; // Please note that the function called here is a virtual function, and the pointer to the member function of the magic class really supports polymorphism. 
 cout << (b. * fun) (4,5) << endl << endl; 


 // fun = & B :: func2; // This type of error drops, because there is no "pointer to the class member function of the derived class" The implicit conversion of "pointer to class member function" to base class 
 fun = (int (A :: *) (int, int)) & B :: func2; // 
 cout << (a. * fun) (4,5) << endl; 
 cout << (b. * fun) (4,5) << endl << endl; 
 
 p0 fun0 = & B :: func2; 
 cout << (a. * fun) (4,5) << endl; 
 cout << (b. * Fun) (4,5) << endl <<
 

 

 // From the above, it is not difficult to find that the relationship between the pointer base class and the derived class pointing to the class member function is completely opposite to the relationship between the pointer base class and the derived class pointing to the class object,
 // The layout of the base class member function is considered to be a derived class A subset of the member function layout
 return 0;
}

Next is the use of pointers to class member functions of the template class

Examples are as follows:

#include <tchar.h>
#include <iostream>
#include <stdio.h>
using namespace std;


class A
{
public:
 int func(int x,int y)
 {
  printf("A::func : x=%d,y=%d/n",x,y);
  return (x<y?x:y);
 }
};
class B
{
public:
 int func(int x,int y)
 {
  printf("B::func : x=%d,y=%d/n",x,y);
  return (x>y?x:y);
 }
};

template<class T>
class C
{
public:
 T c;
 void Print()
 {
  int (T::*p)(int,int)=&T::func;
  (c.*p)(4,5);
 }
};

int main()
{
 C<A> ca;
 C<B> cb;

 ca.Print();
 cb.Print();
 return 0;
}

It can be clearly seen from above. . In fact, it is no different from ordinary templates. . It's just that the qualified name should be OK. . .

Hey. . .

 

 

Guess you like

Origin www.cnblogs.com/lh03061238/p/12693455.html