C++ const char* 和 char const* - 它们相同吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8091770/
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
const char* and char const* - are they the same?
提问by Luchian Grigore
From my understanding, const
modifiers should be read from right to left. From that, I get that:
根据我的理解,const
修饰符应该从右到左阅读。从中,我明白了:
const char*
is a pointer whose char elements can't be modified, but the pointer itself can, and
是一个指针,其字符元素不能被修改,但指针本身可以,并且
char const*
is a constant pointer to mutable
chars.
是指向mutable
字符的常量指针。
But I get the following errors for the following code:
但是我收到以下代码的以下错误:
const char* x = new char[20];
x = new char[30]; //this works, as expected
x[0] = 'a'; //gives an error as expected
char const* y = new char[20];
y = new char[20]; //this works, although the pointer should be const (right?)
y[0] = 'a'; //this doesn't although I expect it to work
So... which one is it? Is my understanding or my compiler(VS 2005) wrong?
那么是哪一个呢?我的理解还是我的编译器(VS 2005)错了?
回答by Greyson
Actually, according to the standard, const
modifies the element directly to its left. The use of const
at the beginning of a declaration is just a convenient mental shortcut. So the following two statements are equivalent:
实际上,根据标准,const
直接修改元素的 left。const
在声明的开头使用只是一个方便的心理捷径。所以下面两个语句是等价的:
char const * pointerToConstantContent1;
const char * pointerToConstantContent2;
In order to ensure the pointer itself is not modified, const
should be placed after the asterisk:
为了保证指针本身不被修改,const
应该放在星号之后:
char * const constantPointerToMutableContent;
To protect both the pointer and the content to which it points, use two consts.
要保护指针和它指向的内容,请使用两个常量。
char const * const constantPointerToConstantContent;
I've personally adopted alwaysputting the const after the portion I intend not to modify such that I maintain consistency even when the pointer is the part I wish to keep constant.
我个人采用始终将 const 放在我不打算修改的部分之后,这样即使指针是我希望保持不变的部分,我也能保持一致性。
回答by iammilind
It works because both are same. May be you confused in this,
它有效,因为两者是相同的。可能你对此感到困惑,
const char* // both are same
char const*
and
和
char* const // unmutable pointer to "char"
and
和
const char* const // unmutable pointer to "const char"
[To remember this, here is a simple rule, '*' affects its whole LHS first]
[记住这一点,这里有一个简单的规则,'*'首先影响它的整个LHS]
回答by Akanksh
That is because the rule is:
那是因为规则是:
RULE: const
binds left, unless there is nothing on the left, then it binds right :)
规则:const
绑定左边,除非左边没有任何东西,然后绑定右边:)
so, look at these as:
所以,看看这些:
(const --->> char)*
(char <<--- const)*
both same! oh, and --->>
and <<---
are NOT operators, they just show what the const
binds to.
都一样!哦,并且--->>
和<<---
不是运算符,它们只是显示const
绑定到的内容。
回答by Sebastian Mach
(from 2 simple variable initialization question)
(来自2 个简单的变量初始化问题)
A really good rule of thumb regarding const
:
一个非常好的经验法则const
:
Read Declarations Right-to-Left.
从右到左阅读声明。
(see Vandevoorde/Josutiss "C++ Templates: The Complete Guide")
(参见 Vandevoorde/Josutiss“C++ 模板:完整指南”)
E.g.:
例如:
int const x; // x is a constant int
const int x; // x is an int which is const
// easy. the rule becomes really useful in the following:
int const * const p; // p is const-pointer to const-int
int const &p; // p is a reference to const-int
int * const * p; // p is a pointer to const-pointer to int.
Ever since I follow this rule-of-thumb, I never misinterpreted such declarations again.
自从我遵循这条经验法则以来,我再也没有误解过这样的声明。
(: sisab retcarahc-rep a no ton ,sisab nekot-rep a no tfel-ot-thgir naem I hguohT :tidE
(: sisab retcarahc-rep a no ton , sisab nekot-rep a no tfel-ot-thgir naem I hguohT :tidE
回答by yasouser
Here is how I always try to interpret:
这是我总是试图解释的方式:
char *p
char *p
|_____ start from the asterisk. The above declaration is read as: "content of `p` is a `char`".
char * const p
char * const p
|_____ again start from the asterisk. "content of constant (since we have the `const`
modifier in the front) `p` is a `char`".
char const *p
char const *p
|_____ again start from the asterisk. "content of `p` is a constant `char`".
Hope it helps!
希望能帮助到你!
回答by Lino Mediavilla
In both of your cases you're pointing to a constant char.
在您的两种情况下,您都指向一个常量字符。
const char * x //(1) a variable pointer to a constant char
char const * x //(2) a variable pointer to a constant char
char * const x //(3) a constant pointer to a variable char
char const * const x //(4) a constant pointer to a constant char
char const * const * x //(5) a variable pointer to a constant pointer to a constant char
char const * const * const x //(6) can you guess this one?
By default, const
applies to what is inmediately at is left, but it could apply to what is inmediately at its right if there's nothing preceeding it, as in (1).
默认情况下,const
适用于位于左侧的中间位置,但如果前面没有任何内容,则它可以应用于位于其右侧的中间位置,如 (1)。