C++ Const Usage Explanation(C++ 常量使用说明)
问题描述
const int* const Method3(const int* const&) const;
有人可以解释每个 const 的用法吗?
Can someone explain the usage of each of the const?
推荐答案
阅读:https://isocpp.org/wiki/faq/const-正确性
最后的const
表示函数Method3
不会修改其类的不可变成员.
The final const
means that the function Method3
does not modify the non mutable members of its class.
const int* const
表示指向常量int的常量指针:即不能改变的指针,指向不能改变的int:this和const int&的唯一区别;
是可以null
const int* const
means a constant pointer to a constant int: i.e. a pointer that cannot be changed, to an int that cannot be changed: the only difference between this and const int&
is that it can be null
const int* const&
表示对指向常量 int 的常量指针的引用.通常指针不是通过引用传递的;const int* &
更有意义,因为这意味着可以在方法调用期间更改指针,这是我可以看到通过引用传递指针 const 的唯一原因int* const&
在所有意图和目的上都与 const int* const
相同,只是它的效率可能较低,因为指针是普通旧数据 (POD) 类型,这些应该在一般按值传递.
const int* const&
means a reference to a constant pointer to a constant int. Usually pointers are not passed by reference; const int* &
makes more sense because it would mean that the pointer could be changed during the method call, which would be the only reason I can see to pass a pointer by reference, const int* const&
is to all intents and purposes the same as const int* const
except that it is probably less efficient as pointers are plain old data (POD) types and these should, in general be passed by value.
这篇关于C++ 常量使用说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 常量使用说明
- OpenGL 对象的 RAII 包装器 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 将函数的返回值分配给引用 C++? 2022-01-01
- GDB 不显示函数名 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- 将 hdc 内容复制到位图 2022-09-04
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- XML Schema 到 C++ 类 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01