C++ function pointer details and code sharing
- 2020-06-01 10:27:16
- OfStack
A function pointer
Functions are stored in code areas of memory, which also have addresses. If we have an int test(int a) function, then its address is the name of the function, just as the name of the array is the starting address of the array.
1. Definition of function pointer: data_types (*func_pointer)(data_types arg1, data_types arg2... , data_types argn);
c language function pointer definition form: return type (* function pointer name)(parameter type, parameter type, parameter type,...) ;
c++ function pointer definition form: return type (class name ::* function member name) (parameter type, parameter type, parameter type,... .);
For example: int (*fp)(int a); // here we define a pointer fp to a function (the function parameter is only of type int, and the return value of the function is of type int).
A pointer to a class member function is not the same as a pointer to a normal function. The first one is going to be.* and -
>
The * operator is used, while the latter can be used with the * operator (called "dereferencing" dereference, or "inter-address" indirection).
The normal function pointer actually holds the starting address of the function body, so it is also called a "code pointer" to distinguish it from the most commonly used data pointer C/C++.
The pointer of the class member function is not only the memory starting address of the class member function, but also needs to be able to solve the adjustment problem of the class instance address caused by the multiple inheritance and virtual inheritance of C++. Therefore, the pointer of the class member function must pass in the class instance object when it is called.
Function pointer example
#include <stdio.h>
#include <stdlib.h>
int fun1()
{
printf("this is fun1 call\n");
return 1;
}
void fun2(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
}
int main()
{
int (*pfun1)() = NULL;
void (*pfun2)(int, char) = NULL;
int a,b;
pfun1 = fun1; // The first 1 A method of assignment
a = pfun1(); // The first 1 Type of invocation method (recommended)
printf("%d\n",a);
b = (*pfun1)();// The first 2 Kind of call method
printf("%d\n",b);
pfun2 = &fun2;// The first 2 A variety of assignment methods (recommended because and other data pointer assignment methods 1 To)
pfun2(1,'a');
(*pfun2)(2,'b');
return 0;
}
Function pointer as function parameter:
#include <stdio.h>
#include <stdlib.h>
void fun(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
}
void fun1(void (*pfun)(int, char), int a, char c)
{
pfun(a, c);
}
int main()
{
fun1(fun, 1, 'a');
return 0;
}
// c++ The form is similar
Function pointer as function return value:
// c In the form of
#include <stdio.h>
#include <stdlib.h>
void fun(int k, char c)
{
printf("this is fun2 call:%d %c\n", k, c);
}
//fun1 The parameter of the function is double , the return value is the function pointer void(*)(int, char)
void (*fun1(double d))(int, char)
{
printf("%f\n",d);
return fun;
}
int main()
{
void (*p)(int, char) = fun1(3.33);
p(1, 'a');
return 0;
}
//c++ In the form of
#include <iostream>
using namespace std;
class test
{
public:
int fun(int a, char c)
{
cout<<"this is fun call:"<<a<<" "<<c<<endl;
return a;
}
};
class test2
{
public:
// test2 Is a member function of fun1, The parameter is double .
// The return value is test Is a pointer to a member function int(test::*)(int, char)
int (test::*fun1(double d))(int, char)
{
cout<<d<<endl;
return &test::fun;
}
};
int main()
{
test mytest;
test2 mytest2;
int (test::*p)(int, char) = mytest2.fun1(3.33);
(mytest.*p)(1, 'a');
return 0;
}
Function pointer array:
#include <stdio.h>
#include <stdlib.h>
float add(float a,float b){return a+b;}
float minu(float a,float b){return a-b;}
int main()
{
// define 1 An array of function Pointers with a size of 2
// Inside the store float (*)(float, float) Pointer to type
float (*pfunArry[2])(float, float) = {&add, &minu};
double k = pfunArry[0](3.33,2.22);// call
printf("%f\n", k);
k = pfunArry[1](3.33,2.22);
printf("%f\n", k);
return 0;
}
//c++ analogous
typedef simplified function pointer type:
#include <stdio.h>
#include <stdlib.h>
float add(float a,float b)
{
printf("%f\n",a+b);
return a+b;
}
float minu(float a,float b)
{
printf("%f\n",a-b);
return a-b;
}
// with pfunType To represent the float(*)(float, float)
typedef float(*pfunType)(float, float);
int main()
{
pfunType p = &add;// Define function pointer variables
p(3.33, 2.22);
pfunType parry[2] = {&add, &minu};// Defines an array of function Pointers
parry[1](3.33, 2.22);
// Function pointer can be defined as: void fun(pfunType p)
// Function pointer as the return value can be defined as: pfunType fun();
return 0;
}
//c++ analogous
conclusion
That's all for the function Pointers and code examples in C++. Interested friends can continue to see the C language implementation of the student course selection system code sharing, you can leave a message at any time, this site will promptly reply to you.