const int const&和const int&in C++有什么区别?
Whats the difference between const int const& and const int& in C++?
在C 中,我可以为常量指针编写const int const*
。我也可以为const引用编写const int&
。const int const&
是对const int的引用?
预先感谢!
const int const*
不是恒定的指针到常数int
。为了获得您需要
const int * const.
在您的情况下,您将两个const
都应用于int
,因此您有一个可变的指针到常量的int
。
const int const&
也发生了同样的事情。为了持续参考常数int
,您需要
const int & const
但这是非法的。
请注意,在const int const*
和const int const&
中,两次应用const
实际上不是合法的。
const int const&对const int的引用?
不,这是一个汇编错误。
尝试编译以下内容:
int z = 0;
const int const& a= z;
,您会看到编译器会向您大喊大叫。
您无法重复相同类型的const
。您的前提是const int const*
是常数指向常数int
也是错误的。
如果您陷入了理解此类声明时,最好像顺时针/螺旋规则那样向后阅读它。
int const * // pointer to const int
int * const // const pointer to int
int const * const // const pointer to const int
int * const * p3; // pointer to const pointer to int
const int * const * p4; // pointer to const pointer to const int
相关文章:
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 在类 (C++) 之外设置 const int 成员变量
- C++ 无法从 const int* 转换为 const_iterator
- 在C++中,如果"int a = 3; int* p = &a;",那么为什么不允许"const int* &pp = p",但允许"const int* const &pp = p"?
- C++如何将 std::vector 转换为 const<int> std::vector<const int>?
- 如何在字符串向量中指向const int
- 如何在 std::map<const int、int> C++ 中重载插入运算符>>?
- 为什么允许 int 和 const int 使用不同的转换函数?
- 如何通过头文件中的函数初始化 const int 数组?
- 为什么 const int 适用于字符大括号初始化?
- 不同类型的"decltype((const int)a)"和"decltype((cons
- 为什么我不能使用与"常量字符*"相同的"const int*"创建一个 int 数组?
- Const Int 宣布与 stoi 不被视为 const
- 传递 const int* 和 int* 时调用重载函数的不同版本(const int* const&/&&)
- 杀死幻数:"const int" vs "constexpr int"(或者最后没有区别)
- 作为返回类型"const int&"做什么?
- 将const int std ::数组的n元素复制到int矢量中.怎么做
- 尝试使用 const int std::array 时出错
- 构造函数中没有从 'const int [5][4]' 到 'const int **' 的已知转换