First, a function pointer is a pointer to a set of functions of the same type, and a class member function we can think of similarly that it is a pointer to a member function of the same set of types in the same category, and of course the member functions here are more accurate in that they refer to non-static member functions. The former is directly to the function address, and the latter we can literally know that it is definitely related to the class and object.
function pointer instance:
Copy Code code as follows:
the typedef int (*P) (int,int);//defines a function pointer type that accepts two int and returns an int type variable
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;//defines the function pointer and assigns it a function pointer
cout<< "min:" << (*fun) (4,5) <<endl;//Why does *fun need to be expanded? Because the operator priority of the * is lower than (), if not () it becomes A * (Fun ())
return 0;
}
The "pointer to a class member function" has a different 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 preceded by the owning type class name A:: Qualified
int main ()
{
P fun=&a::func;
A A; Because the dereference of a member function address must be attached to an object's address, we must create an object.
cout<< "min:" << (A.*fun) (4,5) <<endl;
return 0;
}
Hey.. just use it. * Feel strange.
Next we can expand the following:
Copy Code code as follows:
#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)//pointer name must be preceded by the owning type class name A:: Qualified
typedef int (B::* p0) (int,int);
int main ()
{
A A; Because the dereference of a member function address must be attached to an object's address, we must create an object.
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 virtual function is called here, hey, it's really magical. Class member function pointers also support polymorphism.
cout<< (B.*fun) (4,5) <<endl<<endl;
fun=&b::func2; This type of error drops because there is no implicit conversion of the derived class "pointer to class member function" to the base class "Pointer to a class member function"
fun= (int (A::*) (int,int)) &b::func2;//should be cast
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<<endl;
fun0=&a::func2; Correct, because implicit conversions are made here
cout<< (A.*fun) (4,5) <<endl;
cout<< (B.*fun) (4,5) <<endl<<endl;
From the top, it's not hard to find the relationship between a pointer base class and a derived class that points to a class member function is completely the opposite of the relationship to the pointer base class and derived class of the class object.
The layout of a base class member function is considered to be a subset of the layout of a derived class member function
return 0;
}
Next is the use of the class member function pointer on the template class
Examples are as follows:
Copy Code code 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;
}
From the above you can see clearly. In fact, it is no different from the Normal template. Only the qualified name should be the parameter of the wine OK ...
Hey...