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++ 常量使用说明
基础教程推荐
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01