What may be surprising, is that although the parameter is a const inside the function, the compiler otherwise treats the definition of fcn as if we had defined the parameter as a plain int:
void fcn(const int i) { /* fcn can read but not write to i */ }
void fcn(int i) { /* ... */ } // error: redefines fcn(int)
This usage exists to support compatibility with the C language, which makes no distinction between functions taking const or nonconst parameters.
本文探讨了C++中const参数的使用及其与C语言兼容性的问题。重点介绍了当函数参数被声明为const类型时,编译器如何处理这些参数,并解释了为何即使参数为const,编译器仍将其视为普通整型的情况。
1401

被折叠的 条评论
为什么被折叠?



