我总是搞砸如何正确使用const int*、const int*const和int const*。有没有一套规则来定义你能做什么和不能做什么?
我想知道在赋值、传递给函数等方面的所有注意事项。
我总是搞砸如何正确使用const int*、const int*const和int const*。有没有一套规则来定义你能做什么和不能做什么?
我想知道在赋值、传递给函数等方面的所有注意事项。
当前回答
我在下面画了一张图片来解释这一点,也许会有所帮助。
int const v和const int v是相同的。
其他回答
C++中围绕常量正确性还有许多其他微妙之处。我想这里的问题只是关于C,但我会给出一些相关的例子,因为标记是C++:
通常将字符串等大参数作为TYPE const&传递,这会防止对象被修改或复制。例子:TYPE&TYPE::operator=(const TYPE&rhs){…return*this;}但是TYPE&const是没有意义的,因为引用总是常量。您应该始终将不修改类的类方法标记为const,否则不能从TYPE const引用调用该方法。例子:bool TYPE::运算符==(const TYPE&rhs)const{…}在一些常见情况下,返回值和方法都应该是常量。例子:const TYPE TYPE::运算符+(const TYPE&rhs)const{…}事实上,const方法不能返回内部类数据作为对非常量的引用。因此,必须经常使用常量重载创建常量和非常量方法。例如,如果定义T const&operator[](unsigned i)const;,那么您可能还需要以下给出的非常量版本:内联T运算符[](无符号i)(&O){返回const_cast<char&>(static_cast<const TYPE&>(*this)[](i));}
事实上,C中没有常量函数,非成员函数本身在C++中不能是常量,常量方法可能有副作用,编译器不能使用常量函数来避免重复的函数调用。事实上,即使是一个简单的int const&reference,它引用的值也可能在其他地方更改。
对我来说,常量的位置,即它相对于*是出现在左侧还是右侧,还是同时出现在左侧和右侧,有助于我理解实际含义。
*左边的常量表示指针指向的对象是常量对象。*右边的常量表示指针是常量指针。
下表摘自斯坦福CS106L标准C++编程实验室课程阅读器。
很多人都答对了,我会在这里整理好,并在给出的答案中添加一些缺失的额外信息。
Const是C语言中的关键字,也称为限定符。Const罐应用于任何变量的声明,以指定其值不会改变
const int a=3,b;
a=4; // give error
b=5; // give error as b is also const int
you have to intialize while declaring itself as no way to assign
it afterwards.
如何阅读?
只需从右到左阅读每一条语句即可顺利完成
3件主要事情
type a. p is ptr to const int
type b. p is const ptr to int
type c. p is const ptr to const int
[错误]
if * comes before int
两种类型
1. const int *
2. const const int *
我们先看
主要类型1。常量int*
在3个地方安排3件事的方法3=6.
i.*开始时
*const int p [Error]
*int const p [Error]
二。开始时常量
const int *p type a. p is ptr to const int
const *int p [Error]
iii.开始时int
int const *p type a.
int * const p type b. p is const ptr to int
主要类型2。常量常量int*
在4个地方安排4件事情的方法,其中2件是相同的4件/2!=12
i.*开始时
* int const const p [Error]
* const int const p [Error]
* const const int p [Error]
二。开始时为int
int const const *p type a. p is ptr to const int
int const * const p type c. p is const ptr to const int
int * const const p type b. p is const ptr to int
iii.启动时的常量
const const int *p type a.
const const * int p [Error]
const int const *p type a.
const int * const p type c.
const * int const p [Error]
const * const int p [Error]
挤成一体
类型a.p是常量int(5)的指针
const int *p
int const *p
int const const *p
const const int *p
const int const *p
类型b.p是int(2)的常量指针
int * const p
int * const const p;
类型c.p是const ptr到const int(2)
int const * const p
const int * const p
只是很少的计算
1. const int * p total arrangemets (6) [Errors] (3)
2. const const int * p total arrangemets (12) [Errors] (6)
小小的额外
int常量*p,p2;
here p is ptr to const int (type a.)
but p2 is just const int please note that it is not ptr
int*常量p,p2;
similarly
here p is const ptr to int (type b.)
but p2 is just int not even cost int
int常量*常量p,p2;
here p is const ptr to const int (type c.)
but p2 is just const int.
完成了
我在下面画了一张图片来解释这一点,也许会有所帮助。
int const v和const int v是相同的。
在我读到C++大师斯科特·梅耶斯(Scott Meyers)的这本书之前,我和你一样心存疑虑。请参阅本书中的第三项,其中他详细介绍了如何使用const。
只需遵循以下建议
如果单词const出现在星号的左侧,则所指向的是常量如果单词const出现在星号的右侧,则指针本身是常量如果常量出现在两侧,则两者都是常量