Easy Tutorial
❮ Zookeeper Session Java Print The Triangle ❯

C++ Function Pointers & Class Member Function Pointers

Category Programming Techniques


I. Function Pointers

Functions are stored in the code area of memory, and they also have addresses. If we have a function int test(int a), then its address is the name of the function, which is similar to arrays, where the name of the array is the starting address of the array.

1. Definition of Function Pointers

data_types (*func_pointer)( data_types arg1, data_types arg2, ...,data_types argn);

For example:

int (*fp)(int a); // Here, a pointer fp to a function (the function has only one int type parameter, and the return value is of int type) is defined.

Example

int test(int a)
{
    return a;
}
int main(int argc, const char * argv[])
{

    int (*fp)(int a);
    fp = test;
    cout<&lt;fp(2)&lt;&lt;endl;
    return 0;
}

Note: The function pointed to by the function pointer must maintain the same return value type, number of function parameters, and types.

2. typedef Definition Can Simplify the Definition of Function Pointers

Example

int test(int a)
{
    return a;
}

int main(int argc, const char * argv[])
{

    typedef int (*fp)(int a);
    fp f = test;
    cout<&lt;f(2)&lt;&lt;endl;
    return 0;
}

3. Function Pointers Can Also Be Passed as Parameters to Functions

Example

int test(int a)
{
    return a-1;
}
int test2(int (*fun)(int),int b)
{

    int c = fun(10)+b;
    return c;
}

int main(int argc, const char * argv[])
{

    typedef int (*fp)(int a);
    fp f = test;
    cout<&lt;test2(f, 1)<&lt;endl; // When calling test2, the address of the test function is passed as a parameter to test2
    return 0;
}

Executing the above code, the output result is:

10

4. Using Function Pointers, We Can Form an Array of Function Pointers, More Specifically, an Array of Pointers Pointing to Functions.

Example

void t1(){cout<<"test1"<&lt;endl;}
void t2(){cout<<"test2"<&lt;endl;}
void t3(){cout<<"test3"<&lt;endl;}

int main(int argc, const char * argv[])
{

    typedef void (*fp)(void);
    fp b[] = {t1,t2,t3}; // b[] is an array of pointers pointing to functions
    b[0](); // Using an array of pointers pointing to functions for index operations allows for indirect function calls

    return 0;
}

II. Function Pointers Pointing to Class Member Functions

Definition: A class member function pointer (member function pointer) is a pointer data type in the C++ language, used to store access information for a member function of a specified class with a given parameter list and return value type.

There are basically two points to note:

Here are two examples:

A) Class Member Function Pointers Pointing to Non-Static Member Functions in the Class

For nonstatic member function (non-static member function) to take the address, obtain the actual address of the function in memory

For virtual function (virtual function), its address is unknown at compile time, so taking the address of a virtual member function can only obtain an index value

Example

``` // Function pointer pointing to a class member function

include <iostream>

include <cstdio>

using namespace std;

class A { public: A(int aa = 0):a(aa){}

    ~A(){}

    void setA(int aa = 1)
    {
        a = aa;
    }

    virtual void print()
    {
        cout << "A: " << a << endl;
    }

    virtual void printa()
    {
        cout << "A1: " << a << endl;
    }
private:
    int a;

};

class B:public A { public: B():A(), b(0){}

❮ Zookeeper Session Java Print The Triangle ❯