C++ const int*、const int * const 和 int const * 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1143262/
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
What is the difference between const int*, const int * const, and int const *?
提问by Matt Price
I always mess up how to use const int*
, const int * const
, and int const *
correctly. Is there a set of rules defining what you can and cannot do?
我总是搞砸了怎么用const int*
,const int * const
和int const *
正确的。是否有一套规则定义您可以做什么和不可以做什么?
I want to know all the do's and all don'ts in terms of assignments, passing to the functions, etc.
我想知道在分配、传递给函数等方面的所有注意事项。
回答by Matt Price
Read it backwards (as driven by Clockwise/Spiral Rule):
向后阅读(由顺时针/螺旋规则驱动):
int*
- pointer to intint const *
- pointer to const intint * const
- const pointer to intint const * const
- const pointer to const int
int*
- 指向 int 的指针int const *
- 指向 const int 的指针int * const
- 指向 int 的 const 指针int const * const
- const 指向 const int 的指针
Now the first const
can be on either side of the type so:
现在第一个const
可以在类型的任一侧,所以:
const int *
==int const *
const int * const
==int const * const
const int *
==int const *
const int * const
==int const * const
If you want to go really crazy you can do things like this:
如果你真的想发疯,你可以做这样的事情:
int **
- pointer to pointer to intint ** const
- a const pointer to a pointer to an intint * const *
- a pointer to a const pointer to an intint const **
- a pointer to a pointer to a const intint * const * const
- a const pointer to a const pointer to an int- ...
int **
- 指向 int 指针的指针int ** const
- 指向 int 指针的 const 指针int * const *
- 一个指向 int 的 const 指针的指针int const **
- 一个指向 const int 的指针的指针int * const * const
- 指向 int 的 const 指针的 const 指针- ...
And to make sure we are clear on the meaning of const
:
并确保我们清楚 的含义const
:
int a = 5, b = 10, c = 15;
const int* foo; // pointer to constant int.
foo = &a; // assignment to where foo points to.
/* dummy statement*/
*foo = 6; // the value of a can′t get changed through the pointer.
foo = &b; // the pointer foo can be changed.
int *const bar = &c; // constant pointer to int
// note, you actually need to set the pointer
// here because you can't change it later ;)
*bar = 16; // the value of c can be changed through the pointer.
/* dummy statement*/
bar = &a; // not possible because bar is a constant pointer.
foo
is a variable pointer to a constant integer. This lets you change what you point to but not the value that you point to. Most often this is seen with C-style strings where you have a pointer to a const char
. You may change which string you point to but you can't change the content of these strings. This is important when the string itself is in the data segment of a program and shouldn't be changed.
foo
是指向常量整数的变量指针。这使您可以更改指向的内容,但不能更改指向的值。大多数情况下,这会出现在 C 风格的字符串中,其中您有一个指向const char
. 您可以更改指向的字符串,但不能更改这些字符串的内容。当字符串本身位于程序的数据段中并且不应更改时,这一点很重要。
bar
is a constant or fixed pointer to a value that can be changed. This is like a reference without the extra syntactic sugar. Because of this fact, usually you would use a reference where you would use a T* const
pointer unless you need to allow NULL
pointers.
bar
是指向可以更改的值的常量或固定指针。这就像一个没有额外语法糖的引用。由于这个事实,T* const
除非您需要允许NULL
指针,否则通常您会在使用指针的地方使用引用。
回答by Shijing Lv
For those who don't know about Clockwise/Spiral Rule: Start from the name of the variable, move clockwisely (in this case, move backward) to the next pointeror type. Repeat until expression ends.
对于那些不知道顺时针/螺旋规则的人:从变量的名称开始,顺时针移动(在这种情况下,向后移动)到下一个指针或键入。重复直到表达式结束。
Here is a demo:
这是一个演示:
回答by Kaz Dragon
I think everything is answered here already, but I just want to add that you should beware of typedef
s! They're NOT just text replacements.
我认为这里已经回答了所有问题,但我只想补充一点,您应该提防typedef
s!它们不仅仅是文本替换。
For example:
例如:
typedef char *ASTRING;
const ASTRING astring;
The type of astring
is char * const
, not const char *
. This is one reason I always tend to put const
to the right of the type, and never at the start.
的类型astring
是char * const
,不是const char *
。这就是我总是倾向于将字体放在const
右边的原因之一,而从不放在开头。
回答by luke
Like pretty much everyone pointed out:
就像几乎每个人都指出的那样:
What's the difference between const X* p
, X* const p
and const X* const p
?
const X* p
,X* const p
和 有const X* const p
什么区别?
You have to read pointer declarations right-to-left.
const X* p
means "p points to an X that is const": the X object can't be changed via p.
X* const p
means "p is a const pointer to an X that is non-const": you can't change the pointer p itself, but you can change the X object via p.
const X* const p
means "p is a const pointer to an X that is const": you can't change the pointer p itself, nor can you change the X object via p.
您必须从右到左阅读指针声明。
const X* p
意思是“p 指向一个常量的 X”:X 对象不能通过 p 改变。
X* const p
表示“p 是一个指向非常量 X 的常量指针”:您不能更改指针 p 本身,但您可以通过 p 更改 X 对象。
const X* const p
意思是“p 是一个指向常量 X 的常量指针”:你不能改变指针 p 本身,也不能通过 p 改变 X 对象。
回答by Behrooz Tabesh
Constant reference:
A reference to a variable (here int), which is constant. We pass the variable as a reference mainly, because references are smaller in size than the actual value, but there is a side effect and that is because it is like an alias to the actual variable. We may accidentally change the main variable through our full access to the alias, so we make it constant to prevent this side effect.
int var0 = 0; const int &ptr1 = var0; ptr1 = 8; // Error var0 = 6; // OK
Constant pointers
Once a constant pointer points to a variable then it cannot point to any other variable.
int var1 = 1; int var2 = 0; int *const ptr2 = &var1; ptr2 = &var2; // Error
Pointer to constant
A pointer through which one cannot change the value of a variable it points is known as a pointer to constant.
int const * ptr3 = &var2; *ptr3 = 4; // Error
Constant pointer to a constant
A constant pointer to a constant is a pointer that can neither change the address it's pointing to and nor can it change the value kept at that address.
int var3 = 0; int var4 = 0; const int * const ptr4 = &var3; *ptr4 = 1; // Error ptr4 = &var4; // Error
常数参考:
对变量(此处为 int)的引用,该变量是常量。我们主要将变量作为引用传递,因为引用的大小比实际值要小,但是有一个副作用,那就是它就像是实际变量的别名。我们可能会通过对别名的完全访问而意外更改主变量,因此我们将其设为常量以防止这种副作用。
int var0 = 0; const int &ptr1 = var0; ptr1 = 8; // Error var0 = 6; // OK
常量指针
一旦常量指针指向一个变量,它就不能指向任何其他变量。
int var1 = 1; int var2 = 0; int *const ptr2 = &var1; ptr2 = &var2; // Error
指向常量的指针
一个不能改变它所指向的变量值的指针被称为常量指针。
int const * ptr3 = &var2; *ptr3 = 4; // Error
常量指针
指向常量的常量指针是一个指针,它既不能改变它指向的地址,也不能改变保存在该地址的值。
int var3 = 0; int var4 = 0; const int * const ptr4 = &var3; *ptr4 = 1; // Error ptr4 = &var4; // Error
回答by AProgrammer
The general rule is that the const
keyword applies to what precedes it immediately. Exception, a starting const
applies to what follows.
一般规则是const
关键字适用于它之前的内容。例外,开始const
适用于以下内容。
const int*
is the same asint const*
and means "pointer to constant int".const int* const
is the same asint const* const
and means "constant pointer to constant int".
const int*
int const*
与“指向常量 int 的指针”相同并表示。const int* const
int const* const
与“指向常量 int 的常量指针”相同并表示。
Edit:For the Dos and Don'ts, if this answerisn't enough, could you be more precise about what you want?
编辑:对于该做和不该做的事情,如果这个答案还不够,你能更准确地说明你想要什么吗?
回答by T.E.D.
This question shows preciselywhy I like to do things the way I mentioned in my question is const after type id acceptable?
这个问题恰恰说明了为什么我喜欢按照我在问题中提到的方式做事的原因是 const after type id 可以接受?
In short, I find the easiest way to remember the rule is that the "const" goes afterthe thing it applies to. So in your question, "int const *" means that the int is constant, while "int * const" would mean that the pointer is constant.
简而言之,我发现记住规则的最简单方法是“const”在它适用的事物之后。所以在你的问题中,“int const *”意味着int是常量,而“int * const”意味着指针是常量。
If someone decides to put it at the very front (eg: "const int *"), as a special exception in that case it applies to the thing after it.
如果有人决定把它放在最前面(例如:“const int *”),作为这种情况下的特殊例外,它适用于它之后的事物。
Many people like to use that special exception because they think it looks nicer. I dislike it, because it is an exception, and thus confuses things.
许多人喜欢使用这个特殊的例外,因为他们认为它看起来更好。我不喜欢它,因为它是一个例外,从而使事情变得混乱。
回答by ufukgun
Simple Use of const
.
的简单使用const
。
The simplest use is to declare a named constant. To do this, one declares a constant as if it was a variable but add const
before it. One has to initialize it immediately in the constructor because, of course, one cannot set the value later as that would be altering it. For example:
最简单的用法是声明一个命名常量。要做到这一点,可以像变量一样声明一个常量,但const
在它之前添加。必须在构造函数中立即初始化它,因为当然,不能稍后设置该值,因为这会改变它。例如:
const int Constant1=96;
will create an integer constant, unimaginatively called Constant1
, with the value 96.
将创建一个整数常量,毫无想象力地称为Constant1
,值为 96。
Such constants are useful for parameters which are used in the program but are do not need to be changed after the program is compiled. It has an advantage for programmers over the C preprocessor #define
command in that it is understood & used by the compiler itself, not just substituted into the program text by the preprocessor before reaching the main compiler, so error messages are much more helpful.
这些常量对于程序中使用但在程序编译后不需要更改的参数很有用。对于程序员来说#define
,它比 C 预处理器命令具有优势,因为它可以被编译器本身理解和使用,而不仅仅是在到达主编译器之前被预处理器替换到程序文本中,因此错误消息更有帮助。
It also works with pointers but one has to be careful where const
to determine whether the pointer or what it points to is constant or both. For example:
它也适用于指针,但必须小心在何处const
确定指针或其指向的内容是常量还是两者兼而有之。例如:
const int * Constant2
declares that Constant2
is variable pointer to a constant integer and:
声明它Constant2
是指向常量整数的变量指针,并且:
int const * Constant2
is an alternative syntax which does the same, whereas
是一种替代语法,它的作用相同,而
int * const Constant3
declares that Constant3
is constant pointer to a variable integer and
声明它Constant3
是指向可变整数的常量指针,并且
int const * const Constant4
declares that Constant4
is constant pointer to a constant integer. Basically ‘const' applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to whatever is its immediate right).
声明它Constant4
是指向常量整数的常量指针。基本上,'const' 适用于它紧靠左边的任何东西(除非那里没有任何东西,在这种情况下,它适用于它紧靠右边的任何东西)。
ref: http://duramecho.com/ComputerInformation/WhyHowCppConst.html
参考:http: //duramecho.com/ComputerInformation/WhyHowCppConst.html
回答by rgk
I had the same doubt as you until I came across this bookby the C++ Guru Scott Meyers. Refer the third Item in this book where he talks in details about using const
.
我和你有同样的疑问,直到我看到C++ 大师 Scott Meyers 写的这本书。请参阅本书中的第三项,他详细介绍了使用const
.
Just follow this advice
只要遵循这个建议
- If the word
const
appears to the left of the asterisk, what's pointed to is constant - If the word
const
appears to the right of the asterisk, the pointer itself is constant - If
const
appears on both sides, both are constant
- 如果单词
const
出现在星号的左边,则指向的是常量 - 如果单词
const
出现在星号的右侧,则指针本身是常数 - 如果
const
出现在两边,则两者都是常数
回答by Abhijit Sahu
It's simple but tricky. Please note that we can swap the const
qualifier with any data type (int
, char
, float
, etc.).
这很简单但很棘手。请注意,我们可以交换const
与任何数据类型(预选赛int
,char
,float
,等)。
Let's see the below examples.
让我们看看下面的例子。
const int *p
==> *p
is read-only [p
is a pointer to a constant integer]
const int *p
==>*p
是只读的 [p
是一个指向常量整数的指针]
int const *p
==> *p
is read-only [p
is a pointer to a constant integer]
int const *p
==>*p
是只读的 [p
是一个指向常量整数的指针]
int *p const
==> WrongStatement. Compiler throws a syntax error.
int *p const
==>错误的陈述。编译器抛出语法错误。
int *const p
==> p
is read-only [p
is a constant pointer to an integer].
As pointer p
here is read-only, the declaration and definition should be in same place.
int *const p
==>p
是只读的 [p
是一个指向整数的常量指针]。由于p
这里的指针是只读的,声明和定义应该在同一个地方。
const int *p const
==> WrongStatement. Compiler throws a syntax error.
const int *p const
==>错误的陈述。编译器抛出语法错误。
const int const *p
==> *p
is read-only
const int const *p
==>*p
是只读的
const int *const p1
==> *p
and p
are read-only [p
is a constant pointer to a constant integer]. As pointer p
here is read-only, the declaration and definition should be in same place.
const int *const p1
==>*p
并且p
是只读的 [p
是一个指向常量整数的常量指针]。由于p
这里的指针是只读的,声明和定义应该在同一个地方。
int const *p const
==> WrongStatement. Compiler throws a syntax error.
int const *p const
==>错误的陈述。编译器抛出语法错误。
int const int *p
==> WrongStatement. Compiler throws a syntax error.
int const int *p
==>错误的陈述。编译器抛出语法错误。
int const const *p
==> *p
is read-only and is equivalent to int const *p
int const const *p
==>*p
是只读的,等价于int const *p
int const *const p
==> *p
and p
are read-only [p
is a constant pointer to a constant integer]. As pointer p
here is read-only, the declaration and definition should be in same place.
int const *const p
==>*p
并且p
是只读的 [p
是一个指向常量整数的常量指针]。由于p
这里的指针是只读的,声明和定义应该在同一个地方。