函数声明中的 C++ const 变量

标签 c++ reference constants

<分区>

Possible Duplicate:
What is the difference between const int*, const int * const, and int const *?

我想了解 const 在函数参数中的工作原理。 void function(int const *&i);void function(const int *&i); 有什么区别?如果您能举例说明它们的实现和 i 的用法,我将不胜感激。

最佳答案

它们是一样的,从右往左读就可以了:

First example: i is a reference to a pointer of a constant integer.

Second example: i is a reference to a pointer of an integer constant.

const 的放置只会改变指针两边的内容。例如:

const int* i

意思是:i是一个指向常量整数的指针

int* const i

表示:i是一个指向整数的常量指针。

关于函数声明中的 C++ const 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5861497/

相关文章:

Java:从计算的角度来看,如果将一个数组设置为对另一个数组的引用会怎样?

Java : setting object to null within a method has no effect (Reusing code)

c++ - 当我尝试将一对插入 map 时,Qt 程序出现段错误

c++ - 函数名称修改。 _cdecl约定

使用 std::map 的类 header 中的 C++ 模板化属性 - 编译器错误 C1001

c++ - 如何导出类函数,而不是 DLL 中的整个类

php - 创建两个数组,一个以 0 为索引,另一个以 ID 为索引,并使用连接两者的引用

c++ - 当编译时参数未知时创建 execv 参数数组

actionscript-3 - for(const i)而不是(var i)

C99: "int const *ptr"是什么意思?