General function Pointers and class member function Pointers in depth

  • 2020-04-02 01:28:25
  • OfStack

Function Pointers are called indirectly by Pointers to functions. Function pointer can encapsulate the function with the same parameter type, parameter order and return value. Since there is an invisible this pointer in the nonstatic member function of the class, the pointer to the member function of the class is not represented in the same way as the pointer to the general function.

1, pointer to a general function
The declaration of the function pointer includes the parameter type, order, and return value of the function, and only the matching function address can be assigned to the function pointer. In order to encapsulate the same type of function, you can take the function pointer as the parameter of the generic interface function and call the encapsulated function indirectly through the function pointer.
The following is an example of using a pointer to a function.


#include <iostream.h>

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;
}

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 a member function of a class
The static member function of the class is called in the same way as the ordinary function pointer. However, affected by this pointer, the non-static member function of the class is not compatible with the ordinary function pointer. Also, different classes of this pointer are different, so Pointers to nonstatic member functions of different classes are incompatible. Pointer to a nonstatic member function of a class, and the class name needs to be added when declared.

The following is an example of the use of Pointers to member functions of a class, both static and non-static.


#include <iostream.h>

    class CA;

    
    typedef int (CA::*pClassFun)(int, int);

    
    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;
        }

        
        int Result(pClassFun fun, int a, int b)
        {
            return (this->*fun)(a, b);
        }

    };

    
    int Result(CA* pA, pClassFun fun, int a, int b)
    {
        return (pA->*fun)(a, b);
    }

    
    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;
    }


Related articles: