指针可以声明为指向可变(非常量)数据或指向常量数据的指针。
指针可以定义为指向函数。
我和我的同事正在讨论将“const”与指针一起使用,并且提出了关于使用const
函数指针的问题。
这里有一些问题:
- 指向常量函数的指针与指向非常量函数的指针的含义是什么?
- 函数可以是 const 吗?
- 函数可以是非常量(可变)吗?
- 传递函数指针的正确(安全)语法是什么?
编辑 1:函数指针语法
typedef void (*Function_Pointer)(void); // Pointer to void function returning void.
void function_a(Function_Pointer p_func); // Example 1.
void function_b(const Function_Pointer p_func); // Example 2.
void function_c(Function_Pointer const p_func); // Example 3.
void function_d(const Function_Pointer const p_func); // Example 4.
上述声明是将函数指针视为指向内部类型的指针的示例。
数据、变量或内存指针允许上述组合。
所以问题是:函数指针是否可以具有相同的组合以及指向 const 函数的指针是什么意思(例如示例 2)?