Simple C++ function Pointers tutorial

  • 2020-05-10 18:29:31
  • OfStack

define
Each function occupies a segment of memory, and they have a starting address. The pointer to the function entry address is called a function pointer.

grammar
Data type (* pointer variable name)(parameter table) :


int (*myFunc)(double b, int c);

instructions

The data type in the definition of a function pointer is the type of the return value of the function.
Distinguish between the following two statements:


int (*p)(int a, int b);//p is 1 A pointer to a function whose return value is of integer type 
int *p(int a, int b);//p Is the name of a function whose return value type is an integer pointer 

The pointer variable to the function is not fixed to which function it points to, but only means that it defines a variable of this type, which is specially used to store the entry address of the function. It points to a function whose address is assigned to it in a program.
When assigning a value to a function pointer variable, you simply give the function name, not the argument.
For example, the prototype of the function max is: int max(int x, int y); Pointer p is defined as: int (*p)(int a, int b); p = max; Is used to assign the entry address of the function max to the pointer variable p. In this case, p is the pointer to the function max, so p and max both point to the beginning of the function.
In a program, the pointer variable p can successively point to different functions, but a function cannot be assigned to a non-1 function pointer (that is, a function pointer cannot be directed to a function of its type).
If there are the following functions:


int fn1(int x, int y); int fn2(int x);

Function Pointers are defined as follows:


int (*p1)(int a, int b); int (*p2)(int a);

the


p1 = fn1; // correct 
p2 = fn2; // correct 
p1 = fn2; // Compile error generated 

After a function pointer is defined and pointed to a function, the function can be called either by the function name or by the function pointer (that is, the pointer variable pointing to the function).
Statement: c = (*p)(a, b); // refers to the function (max) pointed to by p. The arguments are a,b, and the function value obtained after the function call is assigned to c.
The function pointer can only point to the entry point of the function, but cannot point to a certain instruction in the middle of the function. *(p+1) cannot be used to represent the next instruction of a function.
One of the common USES of function pointer variables is to pass Pointers as arguments to other functions.

Function Pointers are used as examples
Just look at the code comments


#include <iostream>
using namespace std;

class test
{
public:
  test()
  {
    cout<<"constructor"<<endl;
  }
  int fun1(int a, char c)
  {
    cout<<"this is fun1 call:"<<a<<" "<<c<<endl;
    return a;
  }
  void fun2(double d)const
  {
    cout<<"this is fun2 call:"<<d<<endl;
  }
  static double fun3(char buf[])
  {
    cout<<"this is fun3 call:"<<buf<<endl;
    return 3.14;
  }
};

int main()
{
  //  Class static member function Pointers and c The pointer is used in the same way 
  double (*pstatic)(char buf[]) = NULL;// You don't need to add the class name 
  pstatic = test::fun3; // You can take the address symbol without it 
  pstatic("myclaa");
  pstatic = &test::fun3;
  (*pstatic)("xyz");

  // Ordinary member function 
  int (test::*pfun)(int, char) = NULL; //1 You must add the class name 
  pfun = &test::fun1; //1 Must add the address symbol 
  test mytest;
  (mytest.*pfun)(1, 'a'); // Call is 1 Must add the class object name and * symbol 

  //const  Functions (basic ordinary member functions are the same) 
  void (test::*pconst)(double)const = NULL; //1 Need to add const
  pconst = &test::fun2;
  test mytest2;
  (mytest2.*pconst)(3.33);

//  // A pointer to a constructor or destructor, it looks like it can't. I don't know c++ Does the standard say you can't have Pointers to both of these functions 
//  (test::*pcon)() = NULL;
//  pcon = &test.test;
//  test mytest3;
//  (mytest3.*pcon)();

  return 0;
}


Related articles: