指向常量函数的指针的含义是什么? [英] What is meaning of a pointer to a constant function?

查看:81
本文介绍了指向常量函数的指针的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将指针声明为指向可变(非const)数据或指向常量数据的指针.
可以将指针定义为指向函数.

Pointers can be declared as pointing to mutable (non-const) data or pointer to constant data.
Pointers can be defined to point to a function.

我和我的同事们正在讨论将"const"与指针一起使用,而关于const与函数指针一起使用的问题浮出水面.

My coworkers and I were discussing the use of "const" with pointers and the question came up regarding the use of const with function pointers.

以下是一些问题:

  1. 指向常量函数和指针的指针的含义是什么 指向非恒定函数的指针?
  2. 函数可以是const吗?
  3. 函数可以是非常量(可变)吗?
  4. 传递函数指针的正确(安全)语法是什么?
  1. What is the meaning of a pointer to a constant function versus a pointer to a non-constant function?
  2. Can a function be const?
  3. Can a function be non-const (mutable)?
  4. What is the proper (safe) syntax for passing a function pointer?

函数指针语法

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.

以上声明是将函数指针视为指向内部类型的指针的示例.

The above declarations are examples of treating a function pointer like a pointer to an intrinsic type.

数据,变量或内存指针允许上述组合.
因此问题是:函数指针可以具有相同的组合吗?指向const函数的指针的含义是什么(例如示例2)?

A data, variable or memory pointer allows for the above combinations.
So the questions are: can a function pointer have the same combinations and what is meant by a pointer to a const function (such as Example 2)?

推荐答案

在C语言中,不存在函数为const或其他形式的东西,因此指向const函数的指针毫无意义(尽管不能编译)我没有检查任何特定的编译器.

In C, there's no such thing as a function being const or otherwise, so a pointer to a const function is meaningless (shouldn't compile, though I haven't checked with any particular compiler).

请注意,尽管有所不同,但是您可以具有指向函数的const指针,指向返回const的函数的指针等.基本上,除了函数本身以外的所有东西都可以是const.考虑几个例子:

Note that although it's different, you can have a const pointer to a function, a pointer to function returning const, etc. Essentially everything but the function itself can be const. Consider a few examples:

// normal pointer to function
int (*func)(int);

// pointer to const function -- not allowed
int (const *func)(int);

// const pointer to function. Allowed, must be initialized.          
int (*const func)(int) = some_func;

// Bonus: pointer to function returning pointer to const
void const *(*func)(int);

// triple bonus: const pointer to function returning pointer to const.
void const *(*const func)(int) = func.

就将指针作为参数传递给函数而言,这非常简单.通常,您只希望将指针传递给正确的类型.但是,指向任何类型的函数的指针都可以转换为指向其他某种类型的函数的指针,然后返回其原始类型,并保留原始值.

As far as passing a pointer to a function as a parameter goes, it's pretty simple. You normally want to just pass a pointer to the correct type. However, a pointer to any type of function can be converted to a pointer to some other type of function, then back to its original type, and retain the original value.

这篇关于指向常量函数的指针的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆