C++ 将指针数组作为常量传递
作为常量传递的数组与作为常量传递的数组值之间有什么区别 当每个值都是常量时,向函数传递指针数组时:C++ 将指针数组作为常量传递,c++,pointers,constants,C++,Pointers,Constants,作为常量传递的数组与作为常量传递的数组值之间有什么区别 当每个值都是常量时,向函数传递指针数组时: `void display(Fraction* const ar[], int size);` 一切正常,但当数组为常量时 `void display(const Fraction* ar[], int size);` 编译器在调用函数时出现以下错误: `error C2664: 'display' : cannot convert parameter 1 from 'Fraction *
`void display(Fraction* const ar[], int size);`
一切正常,但当数组为常量时
`void display(const Fraction* ar[], int size);`
编译器在调用函数时出现以下错误:
`error C2664: 'display' : cannot convert parameter 1 from 'Fraction *[3]' to 'const Fraction *[]'`
主要内容:
这是一个很好的例子
请注意,const T*a[]
表示T const*a[]
,即您声明的const
不是数组本身;相反,您声明了一个指向const
项的指针数组
本质上,如果语言提供了隐式转换T**
→ T const**
,则您可能会无意中尝试更改最初声明的const
:
int const v = 666;
int* p;
int** pp = &p;
int const** ppHack = pp; //! Happily not allowed!
*ppHack = &v; // Now p points to the const v...
这是一个很好的例子
请注意,const T*a[]
表示T const*a[]
,即您声明的const
不是数组本身;相反,您声明了一个指向const
项的指针数组
本质上,如果语言提供了隐式转换T**
→ T const**
,则您可能会无意中尝试更改最初声明的const
:
int const v = 666;
int* p;
int** pp = &p;
int const** ppHack = pp; //! Happily not allowed!
*ppHack = &v; // Now p points to the const v...
请告诉我链接的最新情况好吗?我正在并行地用谷歌搜索,但这会有所帮助。@DavidTóth:完成。常见问题解答现在位于。我可以要求更新链接吗?我正在并行地用谷歌搜索,但这会有所帮助。@DavidTóth:完成。常见问题解答现在位于。我假设您不打算在
display
中修改ar
,因此您可以在任何地方添加const
,即分数常量*const ar[]
,这是一种合法的转换。我假设您不打算在display
中修改ar
,因此,您可以在任何地方添加const
,即分数const*const ar[]
,这是一种合法的转换。