何时使用const修饰符? [英] When to use const modifier?

查看:57
本文介绍了何时使用const修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对const修饰符感到困惑,特别是当使用const

char *或char *时。

有些老兄在这里说当你不修改它应该是const char

它在函数里面的内容,我读到某个地方当你

赢了''初始化后修改...


那么我什么时候才能使用?是*错误*我不应该使用const

吗?


提前感谢。

Leonardo。

解决方案

Leonardo Korndorfer写道:





我很容易被const修饰符弄糊涂,特别是当使用const

char *或char *时。

这里的一些家伙说当你的时候它应该是const char不要修改

它在函数里面的内容,当你在初始化后没有修改的时候我读到了某个地方......



两者。


那么我什么时候才能使用其中一个?是*错误*我不应该使用const




当对象是实常数时,不使用const只是一个问题,

就像一个字符串文字。


我在其他情况下,const表达了意图。


-

Ian Collins。


Leonardo Korndorfer写道:





我很容易被const修饰符弄糊涂,特别是当使用const

char *或char *时。

这里有些家伙当你不修改它应该是const char

它在函数内的内容,



限定一个函数参数的const将帮助编译器诊断

任何尝试修改函数中的相关对象。


我在某处读到它时你就是
初始化后不会修改...



这里的行为是一样的。


所以我什么时候才能使用其中一个?



这取决于对象的使用方式。如果该对象只是初始化了
并且此后没有修改,那么你可以使const符合它的声明。如果OTOH你只想要某些函数不修改

数组对象,你可以使用const限定相关参数。这样

其他函数仍然可以修改数组。另一种方法是在将数组传递给函数之前将数组包装在结构中。但是

这不会帮助你捕捉到无意的修改,这是const的主要用途。


是*错误*我不应该使用const

吗?



编号但是利用语言功能让你的工作变得更轻松是有帮助的。


Ian Collins写道:


Leonardo Korndorfer写道:


>

我对const修饰符感到困惑,特别是当使用const
char *或char *时。
有些老兄在这里说了应该是const char当你不修改它内容的函数时,我在某个地方读到它,当你在初始化后不会修改它...



两者。


>所以我什么时候才能使用其中一个?是*错误*不应该使用const
我应该吗?



当对象是实常数时,不使用const只是一个问题,

就像一个字符串文字。



是字符串文字真正的常量,无论这意味着什么?我以为你

只是假装它们是常数,并且这样对待它们是为了防止UB的愤怒。


Hi,

I''m litle confused by the const modifier, particularly when use const
char* or char*.
Some dude over here said it should be const char when you dont modify
it content inside the function, I read somewhere that it when you
won''t modify after its initialization...

So when exactly do I use one or another? Is it *wrong* not use const
when I should?

thanks in advance.
Leonardo.

解决方案

Leonardo Korndorfer wrote:

Hi,

I''m litle confused by the const modifier, particularly when use const
char* or char*.
Some dude over here said it should be const char when you dont modify
it content inside the function, I read somewhere that it when you
won''t modify after its initialization...

Both.

So when exactly do I use one or another? Is it *wrong* not use const
when I should?

Not using const is only a problem when the object is a real constant,
like a string literal.

I other cases, const expresses an intent.

--
Ian Collins.


Leonardo Korndorfer wrote:

Hi,

I''m litle confused by the const modifier, particularly when use const
char* or char*.
Some dude over here said it should be const char when you dont modify
it content inside the function,

Const qualifying a function parameter will help the compiler diagnose
any attempt to modify the concerned object within the function.

I read somewhere that it when you
won''t modify after its initialization...

The behaviour is the same here.

So when exactly do I use one or another?

That depends on how the object will be used. If the object will only be
initialised and not modified thereafter then you can const qualify it''s
declaration. If OTOH you want only certain functions to not modify an
array object, you can const qualify the relevant parameter. This way
other functions can still modify the array. An alternative is to wrap
the array inside a structure before passing it to the function. But
this will not help you catch inadvertent modifications, which is the
main use of const.

Is it *wrong* not use const
when I should?

No. But it''s helpful to take advantage of language features to make your
job easier.


Ian Collins wrote:

Leonardo Korndorfer wrote:

>Hi,

I''m litle confused by the const modifier, particularly when use const
char* or char*.
Some dude over here said it should be const char when you dont modify
it content inside the function, I read somewhere that it when you
won''t modify after its initialization...

Both.

>So when exactly do I use one or another? Is it *wrong* not use const
when I should?

Not using const is only a problem when the object is a real constant,
like a string literal.

Are string literals "real constants", whatever that means? I thought you
were just supposed to pretend that they were constants, and treat them
as such, to prevent the wrath of UB.


这篇关于何时使用const修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆