“const int”与“int const”作为 C++ 和 C 中的函数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/162480/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:22:46  来源:igfitidea点击:

'const int' vs. 'int const' as function parameters in C++ and C

c++cconst

提问by Nils Pipenbrinck

Consider:

考虑:

int testfunc1 (const int a)
{
  return a;
}

int testfunc2 (int const a)
{
  return a;
}

Are these two functions the same in every aspect or is there a difference?

这两个功能在各个方面都相同还是有区别?

I'm interested in an answer for the C language, but if there is something interesting in the C++ language, I'd like to know as well.

我对 C 语言的答案很感兴趣,但如果 C++ 语言中有什么有趣的东西,我也想知道。

回答by Ates Goral

The trick is to read the declaration backwards (right-to-left):

诀窍是向后阅读声明(从右到左):

const int a = 1; // read as "a is an integer which is constant"
int const a = 1; // read as "a is a constant integer"

Both are the same thing. Therefore:

两者都是一回事。所以:

a = 2; // Can't do because a is constant

The reading backwards trick especially comes in handy when you're dealing with more complex declarations such as:

当您处理更复杂的声明时,向后阅读技巧尤其有用,例如:

const char *s;      // read as "s is a pointer to a char that is constant"
char c;
char *const t = &c; // read as "t is a constant pointer to a char"

*s = 'A'; // Can't do because the char is constant
s++;      // Can do because the pointer isn't constant
*t = 'A'; // Can do because the char isn't constant
t++;      // Can't do because the pointer is constant

回答by Konrad Rudolph

const Tand T constare identical. With pointer types it becomes more complicated:

const T并且T const是相同的。对于指针类型,它变得更加复杂:

  1. const char*is a pointer to a constant char
  2. char const*is a pointer to a constant char
  3. char* constis a constant pointer to a (mutable) char
  1. const char*是一个指向常量的指针 char
  2. char const*是一个指向常量的指针 char
  3. char* const是一个指向(可变的)的常量指针 char

In other words, (1) and (2) are identical. The only way of making the pointer (rather than the pointee) constis to use a suffix-const.

换句话说,(1)和(2)是相同的。制作指针(而不是指针对象)的唯一方法const是使用后缀- const

This is why many people prefer to always put constto the right side of the type (“East const” style): it makes its location relative to the type consistent and easy to remember (it also anecdotally seems to make it easier to teach to beginners).

这就是为什么许多人更喜欢总是放在字体const的右侧(“East const”样式):它使相对于字体的位置保持一致且易于记忆(据传闻,它似乎也更容易教给初学者)。

回答by Andru Luvisi

There is no difference. They both declare "a" to be an integer that cannot be changed.

没有区别。它们都声明“a”是一个不能改变的整数。

The place where differences start to appear is when you use pointers.

当您使用指针时,差异开始出现的地方。

Both of these:

这两个:

const int *a
int const *a

declare "a" to be a pointer to an integer that doesn't change. "a" can be assigned to, but "*a" cannot.

将“a”声明为指向不变的整数的指针。“a”可以分配给,但“*a”不能。

int * const a

declares "a" to be a constant pointer to an integer. "*a" can be assigned to, but "a" cannot.

将“a”声明为指向整数的常量指针。“*a”可以分配给,但“a”不能。

const int * const a

declares "a" to be a constant pointer to a constant integer. Neither "a" nor "*a" can be assigned to.

将“a”声明为指向常量整数的常量指针。"a" 和 "*a" 都不能赋值。

static int one = 1;

int testfunc3 (const int *a)
{
  *a = 1; /* Error */
  a = &one;
  return *a;
}

int testfunc4 (int * const a)
{
  *a = 1;
  a = &one; /* Error */
  return *a;
}

int testfunc5 (const int * const a)
{
  *a = 1;   /* Error */
  a = &one; /* Error */
  return *a;
}

回答by Fred Larson

Prakash is correct that the declarations are the same, although a little more explanation of the pointer case might be in order.

Prakash 是正确的,声明是相同的,尽管可能需要对指针情况进行更多解释。

"const int* p" is a pointer to an int that does not allow the int to be changed through that pointer. "int* const p" is a pointer to an int that cannot be changed to point to another int.

“const int* p”是一个指向不允许通过该指针改变int 的int 的指针。"int* const p" 是一个指向 int 的指针,不能更改为指向另一个 int。

See https://isocpp.org/wiki/faq/const-correctness#const-ptr-vs-ptr-const.

请参阅https://isocpp.org/wiki/faq/const-correctness#const-ptr-vs-ptr-const

回答by Emerick Rogul

const intis identical to int const, as is true with all scalar types in C. In general, declaring a scalar function parameter as constis not needed, since C's call-by-value semantics mean that any changes to the variable are local to its enclosing function.

const int与 相同int const,对于 C 中的所有标量类型也是如此。通常,const不需要将标量函数参数声明为 as ,因为 C 的按值调用语义意味着对变量的任何更改对于其封闭函数都是局部的。

回答by Nick Westgate

They are the same, but in C++ there's a good reason to always use const on the right. You'll be consistent everywhere because const member functions mustbe declared this way:

它们是相同的,但在 C++ 中有一个很好的理由总是在右边使用 const。您将在任何地方保持一致,因为必须以这种方式声明const 成员函数:

int getInt() const;

It changes the thispointer in the function from Foo * constto Foo const * const. See here.

它将this函数中的指针从更改Foo * constFoo const * const看这里。

回答by Pat Notz

This isn't a direct answer but a related tip. To keep things straight, I always use the convection "put conston the outside", where by "outside" I mean the far left or far right. That way there is no confusion -- the const applies to the closest thing (either the type or the *). E.g.,

这不是一个直接的答案,而是一个相关的提示。为了保持正直,我总是使用对流“放在const外面”,其中“外面”是指最左边或最右边。这样就不会混淆——const 适用于最接近的事物(类型或*)。例如,



int * const foo = ...; // Pointer cannot change, pointed to value can change
const int * bar = ...; // Pointer can change, pointed to value cannot change
int * baz = ...; // Pointer can change, pointed to value can change
const int * const qux = ...; // Pointer cannot change, pointed to value cannot change

回答by prakash

Yes, they are same for just int

是的,它们是相同的 int

and different for int*

和不同的 int*

回答by user7545

I think in this case they are the same, but here is an example where order matters:

我认为在这种情况下它们是相同的,但这里有一个顺序很重要的例子:

const int* cantChangeTheData;
int* const cantChangeTheAddress;