General function pointers and member function pointers for classes deeply parse _c language

Source: Internet
Author: User

A function pointer indirectly invokes a function by pointing to a pointer to a function. function pointers can be implemented to encapsulate the function of parameter type, parameter order and return value, which is a way to realize polymorphism. Because there is an invisible this pointer in a Non-static member function of a class, the pointer to a member function of a class and the pointer to a generic function are not the same.

1, pointing to the general function of the pointer
The function pointer's declaration includes the function's parameter type, order, and return value, and only the matching function address can be assigned to the function pointer. To encapsulate a function of the same type, the function pointer can be used as the parameter of a generic interface function, and the encapsulated function is invoked indirectly through the function pointer.
Here is an example of the use of pointers to functions.

Copy Code code as follows:

#include <iostream.h>
/* Pointer to function/*
typedef int (*pfun) (int, int);
int Max (int a, int b)
{
Return a > B? A:B;
}
int Min (int a, int b)
{
Return a < b? A:B;
}
/* Universal interface function, to implement the encapsulation of other functions.
int result (pfun fun, int a, int b)
{
Return (*fun) (A, b);
}
void Main ()
{
int a = 3;
int b = 4;
cout<< "Test function pointer:" <<endl;
cout<< "The maximum number between A and B is" <<result (Max, A, b) <<endl;
cout<< "The minimum number between A and B is" <<result (Min, A, b) <<endl;
}

2, pointer to the member function of the class
A static member function of a class takes the same invocation as a generic function pointer, and the Non-static member function of a class is incompatible with a generic function pointer, affected by the this pointer. Also, the this pointer is not the same as the same, so pointers to non-static member functions of different classes are incompatible. A pointer to a non-static member function of a class, and you need to add the class name when you declare it.

The following is an example of the use of a pointer to a member function of a class, including the use of pointers to static and non-static member functions.

Copy Code code as follows:

#include <iostream.h>

Class CA;

/* Pointer to non-static member function of class
typedef int (CA::* pclassfun) (int, int);

/* Pointer to general function/*
typedef int (*pgeneralfun) (int, int);

Class CA
{
Public

int Max (int a, int b)
{
Return a > B? A:B;
}

int Min (int a, int b)
{
Return a < b? A:B;
}

static int Sum (int a, int b)
{
return a + B;
}

/* Class internal interface functions, to implement the class of Non-static member functions of the package * *
int result (pclassfun fun, int a, int b)
{
Return (This->*fun) (A, b);
}

};

/* Class external interface functions, to implement the class of Non-static member functions of the package * *
int result (ca* PA, pclassfun fun, int a, int b)
{
Return (Pa->*fun) (A, b);
}

/* Class external interface functions, to implement the class of static member functions of the package * *
int Generalresult (pgeneralfun fun, int a, int b)
{
Return (*fun) (A, b);
}


void Main ()
{
CA CA;
int a = 3;
int b = 4;

cout<< "Test nonstatic member function pointer from member function:" <<endl;
cout<< "The maximum number between A and B is" <<ca. Result (Ca::max, A, b) <<endl;
cout<< "The minimum number between A and B is" <<ca. Result (Ca::min, A, b) <<endl;

cout<<endl;
cout<< "Test nonstatic member function pointer from external function:" <<endl;
cout<< "The maximum number between A and B is" <<result (&ca, Ca::max, A, b) <<endl;
cout<< "The minimum number between A and B is" <<result (&ca, Ca::min, A, b) <<endl;

cout<<endl;
cout<< "Test static member function pointer:" <<endl;
cout<< "The sum of A and B is" <<generalresult (Ca::sum, A, b) <<endl;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.