C++ Const 用法说明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5598703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Const Usage Explanation
提问by RoR
const int* const Method3(const int* const&) const;
Can someone explain the usage of each of the const?
有人可以解释每个常量的用法吗?
采纳答案by satnhak
Read this: https://isocpp.org/wiki/faq/const-correctness
阅读:https: //isocpp.org/wiki/faq/const-correctness
The final const
means that the function Method3
does not modify the non mutable members of its class.
finalconst
意味着该函数Method3
不会修改其类的非可变成员。
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 的常量指针:即无法更改的指针,指向无法更改的 int:唯一的区别const int&
是它可以是null
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.
const int* const&
表示对指向常量 int 的常量指针的引用。通常指针不是通过引用传递的;const int* &
更有意义,因为这意味着可以在方法调用期间更改指针,这将是我可以看到通过引用传递指针的唯一原因,const int* const&
对于所有意图和目的都相同,const int* const
只是它可能效率较低因为指针是普通的旧数据 (POD) 类型,这些通常应该按值传递。
回答by ildjarn
It's easier to understand if you rewrite that as the completely equivalent
如果您将其重写为完全等效的内容,则更容易理解
// v───v───v───v───v───v───v───v───v───v───v───v─┬┐
// ││
// v──#1 v─#2 v──#3 v─#4 #5
int const * const Method3(int const * const&) const;
then read it from right to left.
然后从右到左阅读。
#5 says that the entire function declaration to the left is const
, which implies that this is necessarily a member function rather than a free function.
#5 表示左边的整个函数声明是const
,这意味着 this 必然是成员函数而不是自由函数。
#4 says that the pointer to the left is const
(may not be changed to point to a different address).
#4 表示向左的指针是const
(可能不会更改为指向不同的地址)。
#3 says that the int
to the left is const
(may not be changed to have a different value).
#3 表示int
左边的是const
(可能不会更改为具有不同的值)。
#2 says that the pointer to the left is const
.
#2 表示向左的指针是const
。
#1 says that the int
to the left is const
.
#1 表示int
左边的是const
.
Putting it all together, you can read this as a const
member function named Method3
that takes a reference to a const
pointer to an int const
(or a const int
, if you prefer) and returns a const
pointer to an int const
(const int
).
把它们放在一起,你可以把它读作一个const
名为的成员函数Method3
,它引用一个const
指向 an int const
(或 a const int
,如果你喜欢)的const
指针,并返回一个指向 an int const
( const int
)的指针。
(N.b. #2 is entirely superfluous.)
(注意 #2完全是多余的。)
回答by Alexander Gessler
First of all const T
is equivalent to T const
.
首先const T
等价于T const
.
const int* const
is therefore equivalent to int const * const
.
const int* const
因此等价于int const * const
。
When reading expressions with lots of const
tokens and pointers in them, always try to read them from right to left(after applying the transformation above). So in this case the return value is a const pointer to a const int
. Making the pointer itself const
makes no sense here since the return value isn't a lvalue that could be modified. Making the pointee const
, however, guarantees that the caller may not modify the int
(or array of int
s) returned by Method3
.
在读取包含大量const
标记和指针的表达式时,请始终尝试从右到左读取它们(应用上述转换之后)。所以在这种情况下,返回值是一个指向 constint
的const 指针。在const
这里制作指针本身没有意义,因为返回值不是可以修改的左值。const
但是,使被指点者保证调用者不能修改由返回的int
(或int
s数组)Method3
。
const int*const&
becomes int const*const&
, so it is a reference to a const pointer to a const int
. Passing a const pointer by references male no sense either - you can't modify the referenced value since the pointer is const
and references and pointers occupy equal storage so there aren't any space savings either.
const int*const&
变成int const*const&
,所以它是对指向 const 的 const 指针int
的引用。通过引用传递 const 指针也没有意义 - 您不能修改引用的值,因为指针是const
并且引用和指针占用相同的存储空间,因此也没有任何空间节省。
The last const
indicates that the method does not modify the this
object. The this
pointer within the method body will have the (theoretical) declaration T const * const this
. This means that a const T*
object will be able to call T::Method3()
.
最后一个const
表示该方法不修改this
对象。this
方法体内的指针将具有(理论上的)声明T const * const this
。这意味着一个const T*
对象将能够调用T::Method3()
.
回答by Yony
An easy way to remember the rules of const
is to think about it this way: const
applies to the thing on its left, unless there's nothing on its left.
记住规则的一种简单方法const
是这样考虑:const
适用于其左侧的事物,除非其左侧没有任何东西。
So in the case of const int * const
, the first const has nothing on its left, so it applies to int
and the second one does have something on its left, so it applies to the pointer.
所以在 的情况下const int * const
,第一个 const 在它的左边没有任何东西,所以它适用于int
第二个const 在它的左边确实有一些东西,所以它适用于指针。
This rule also tells you what would happen in the case where you have const int const *
. Since both const's apply to int
this expression is redundant and therefore invalid.
此规则还告诉您在具有const int const *
. 由于这两个 const 都适用于int
这个表达式是多余的,因此是无效的。
回答by justin
const /* don't modify the int or array of ints' value(s) */
int* const /* as a retval, ignored. useless declaration */
Method3(const /* don't modify the int or array of ints' value(s) */
int* const /* don't modify the pointer's value, the address to which `pointer` points to. e.g. you cannot say `++pointer` */
&) const; /* this method does not modify the instance/object which implements the method */
回答by Jason
I like to use the "clock" or "spiral" methodwhere starting from the identifier name (in this case Method3
) you read back-and-forth from left-to-right-back-to-left, etc. in order to decode naming conventions. So const int* const Method3(const int* const&) const
is a class method that doesn't change any class members (of some un-named class) and takes a constant reference to a pointer that points to a constant int
and returns a constant pointer to a constant int
.
我喜欢使用“时钟”或“螺旋”方法,从标识符名称(在本例中Method3
)开始,您从左到右,从后到左等来回读取以解码命名约定。所以const int* const Method3(const int* const&) const
是一个类方法不改变任何类成员(的一些未命名的类),并采用一定的基准的指针,它指向一个常数int
,并返回一个常量指针为恒定int
。
Hope this helps,
希望这可以帮助,
Jason
杰森
回答by Albert Chen
An easy way to remember the const in C++ is when you see some code in form like:
记住 C++ 中 const 的一种简单方法是当您看到一些形式如下的代码时:
XXX const;
const YYY;
XXX, YYY will be a constant component,XXX const
form:
XXX, YYY 将是一个常量组件,XXX const
形式:
function ( def var ) const; ------#1
* const; ------#2
const YYY
form:
const YYY
形式:
const int; ------#3
const double;
People usually use these types. When you see "const&"
somewhere, don't feel confused, const is describing something before itself.
so the answer of this problem is self-evident now.
人们通常使用这些类型。当你看到"const&"
某处时,不要感到困惑,const 是在描述某物之前的东西。所以这个问题的答案现在不言而喻了。
const int* const Method3(const int* const&) const;
| | | | |
#3 #2 #3 #2 #1
回答by MrDetective
I only want to mention that const int* const&
is indeed a constant reference to const int*
. For example:
我只想提一下,这const int* const&
确实是对const int*
. 例如:
int i = 0;
int j = 1;
int* p = &i;
int* q = &j;
const int* const& cpref = p;
cpref = q; //Error: assignment of read-only reference 'cpref'
It's also the case for int* const&
, Which means:"A constant reference to int*
".
But const int*&
is a non-constant reference to const int*
.
Hope this helps.
情况也是如此int* const&
,这意味着:“对”的常量引用int*
。
但const int*&
是对 的非常量引用const int*
。
希望这可以帮助。
回答by Jollymorphic
const #1: The pointer returned by Method3 refers to a const int.
const #1:Method3 返回的指针指向一个 const int。
const #2: The pointer value returned by the function, itself, is const. This is a useless const (though gramatically valid), because the return value from a function cannot be an l-value.
const #2:函数返回的指针值本身是 const。这是一个无用的常量(尽管在语法上有效),因为函数的返回值不能是左值。
const #3: The pointer type passed by reference to the function points to a const int.
const #3:通过引用传递给函数的指针类型指向一个 const int。
const #4: The pointer value passed by reference to the function is, itself, a const pointer. Declaring a value that is passed to a function as const would normally be pointless, but this value is passed by reference, so it can be meaningful.
const #4:通过引用传递给函数的指针值本身就是一个 const 指针。将传递给函数的值声明为 const 通常是没有意义的,但该值是通过引用传递的,因此它可能是有意义的。
const #5: The function (presumably a member function) is const, meaning that it is not allowed to (a) assign new values to any members of the object of which it is part or (b) call a non-const member function on the object or any of its members.
const #5:函数(大概是成员函数)是 const,这意味着它不允许 (a) 为其所属对象的任何成员分配新值或 (b) 调用非常量成员函数在对象或其任何成员上。
回答by Nick Strupat
Reading from right to left makes understanding modifiers easier.
从右到左阅读使理解修饰符更容易。
A const method that takes a reference to a const pointer to a const int called Method3
which returns a const pointer to a const int.
一个 const 方法,它接受一个指向 const int 的 const 指针的引用,Method3
该方法返回一个指向 const int 的 const 指针。
- A const method can't modify members
(unless they are expicitly
mutable
) - A const pointer can't be changed to point to something else
- A const int (or any other type) can't be modified
- const 方法不能修改成员(除非它们是明确的
mutable
) - 不能将 const 指针更改为指向其他内容
- 不能修改 const int(或任何其他类型)