在C ++和C中将'const int'与'int const'作为函数参数 [英] 'const int' vs. 'int const' as function parameters in C++ and C
问题描述
考虑:
int testfunc1(const int a)
{
return a;
}
int testfunc2(int const a)
{
return a;
}
这两个功能在各个方面是否都相同还是有区别? / p>
我对C语言的答案很感兴趣,但是如果C ++语言中有一些有趣的东西,我也想知道。
const T
和 T const
相同。使用指针类型会变得更加复杂:
-
const char *
是指向常量char
-
char const *
是指向常量char
-
char * const
是指向(可变)char
换句话说,(1)和(2)是相同的。使指针(而不是指针) const
的唯一方法是使用后缀- const
。
这就是为什么许多人总是将 const
放在类型右侧的原因( East const样式) :它使它相对于类型的位置一致且易于记忆(从轶事上看,它也使得对初学者的学习更容易)。
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.
const T
and T const
are identical. With pointer types it becomes more complicated:
const char*
is a pointer to a constantchar
char const*
is a pointer to a constantchar
char* const
is a constant pointer to a (mutable)char
In other words, (1) and (2) are identical. The only way of making the pointer (rather than the pointee) const
is to use a suffix-const
.
This is why many people prefer to always put const
to 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).
这篇关于在C ++和C中将'const int'与'int const'作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!