how to use pointer -> function()
时间: 2024-05-15 12:18:08 浏览: 87
To use a pointer to a function in C++, first you need to declare the function pointer type. You can declare the function pointer type using the syntax:
```
return_type (*pointer_name)(arg_type1, arg_type2, ...);
```
Here, `return_type` is the return type of the function pointed to, `arg_type1`, `arg_type2`, etc. are the types of the function parameters, and `(*pointer_name)` declares a function pointer variable called `pointer_name`.
Once you have declared your function pointer variable, you can assign it the address of a function using the `&` operator:
```
pointer_name = &function_name;
```
To call the function using the function pointer, you need to use the dereference operator `(*pointer_name)`:
```
(*pointer_name)(arg1, arg2, ...);
```
Here, `arg1`, `arg2`, etc. are the arguments passed to the function.
In summary, to use a pointer to a function in C++, you need to declare a function pointer type, assign it the address of a function using the `&` operator, and call the function using the dereference operator `(*pointer_name)`.
阅读全文