c++最终常量字符串
在Java中,我们可以将一个字符串指定为final
来声明“常量”。例如,
static final String myConst = "Hello";
在 C++ 中这样做的正确方法是这样的吗?
const char * const myConst = "Hello";
我总是看到人们这样做:
const char * myConst = "Hello";
但是,实际上,您可以更改该指针指向的内容。那么,为什么人们不将指针也声明为常量呢?正确的做法是什么?
In Java, we can specify a string as final
to declare 'constants'. For example
static final String myConst = "Hello";
Is the correct way to do this in c++ like this?
const char * const myConst = "Hello";
I've always seen people do this:
const char * myConst = "Hello";
But, actually, you can change what that pointer points to. So, why do people not declare the pointer as constant as well? What is the correct way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,
const char* const
是声明您不会更改的 C 样式字符串的正确方法。或者更好:
Yes,
const char* const
is the correct way to declare a C-style string which you will not change.Or better:
这意味着所指向的对象不能改变。
这意味着指针指向的位置不能更改,但对象的值可以更改。
这意味着两者都不会改变。根据我的经验,没有人记得这一点,但它总是可以在网上找到!
典型的是,在我写我的文章时三个人回答!
This means the object pointed at cannot change.
This means the location pointed at by the pointer cannot change, but the object's value can.
This means neither may change. In my experience no one remembers this, but it's always available on the net!
Typical, three people answer in the time I write mine!
我不太明白你的问题。为了或多或少地模仿
final
Java 关键字,它可以是(如果指针不会改变),和/或:
如果指针之后可能会改变。最后,请注意,在第一个版本中,您实际上无法更改指针本身,因为它是常量。
I don't exactly understand your question. To more or less mimic the
final
Java keyword, it would be either(if the pointer is not going to change), and/or:
if the pointer may change afterwards. Finally, note that with the first version, you cannot actually change the pointer itself, because it is constant.
根据迭戈的编辑,是的,这是正确的书写方式。人们通常不会声明变量 const,因为他们不关心它是否会被修改,或者更确切地说,相信它不会被修改(因为他们知道他们不会修改它)。
它们声明了指向 const,因为字符串文字确实具有 const 字符,因此您确实必须将其分配给具有 char const * 值的变量。
With Diego's edit, yes, that the right way to write it. People typically don't declare the variable const because they don't care whether it will be modified, or, rather, trust that it won't be modified (since they know they don't modify it).
They declare the pointed-to const because a string literal really has const characters, so you really must assign it to a variable that has char const * values.
从技术上讲,
这是最正确的答案,因为重新分配指针会给您留下一个可能无法恢复的字符串。
某些实现允许您更改“Hello”中的字符(即使这是一个坏主意),因此中的第一个 const
是个好主意。个人认为
和
const char * myCount = ...;
是等价的,我倾向于强制执行一种风格指南,即 const 始终遵循它所修改的项目。从长远来看,它有时会减少误解。
也就是说,大多数人不知道如何在 C++ 中正确使用 const,因此它要么使用得不好,要么根本不使用。那是因为他们只是使用 C++ 作为改进的 C 编译器。
Technically,
is the most correct answer, as reassigning the pointer would leave you with a string which probably could not be recovered.
Some implementations allow you to change the characters in "Hello" (even if it's a bad idea), so the first const in
Is a great idea. Personally as
and
const char * myCount = ...;
are equivalent, I tend to enforce a style guideline that the const always follows the item it modifies. It sometimes reduces misconceptions in the long run.
That said, most people don't know how to use
const
correctly in C++, so it's either used poorly or not at all. That's because they just use C++ as an improved C compiler.因为它很少有用,所以您通常希望堆栈上的值(包括指针)是非常量的。得到萨特& Alexandrescu的《编码标准》一书中,就解释了这一点。
Because it's rarely useful, more often than not you would want to have the values on the stack (pointers included) to be non-const. Get Sutter & Alexandrescu "Coding Standards" book, it explains this point.
真正的要点是,程序员被迫将一些指针至少声明为 const char * 来存储双引号之间的 char 数组。没有什么强迫他们(没有编译器警告或错误)也使指针恒定......因为人们很懒,你可以得出自己的结论。他们甚至可能并没有真正尝试定义一个常量,他们只是想关闭编译器错误(好吧,在本例中是警告)。
记住这一点,无论如何我可能会寻求不同的解决方案:
这里的区别在于,这样我不会将用作字符串的原始字节数组衰减为指针,我仍然会有一个可以准确使用的字节数组作为原始文字。
有了它,我可以执行诸如
sizeof(myConst)
之类的操作,并获得与sizeof("Hello")
相同的结果。如果我将字符串更改为指针,则 sizeof 将返回指针的大小,而不是字符串的大小......显然,当以这种方式执行操作时,更改指针变得毫无意义,因为不再有指针可以更改。
The real point is that programmers are compeled to declare some pointers at least as
const char *
to store char arrays between double quotes. Nothing force them (no compiler warning or error) to also make the pointer constant... as people are lazy you can draw your own conclusion. They are probably not even really trying to define a constant, they just want to shut off compiler errors (well, warning in this case).Keeping that in mind, I would probably go for a different solution anyway:
The difference here is that this way I won't decay the original byte array used as string to a pointer, I will still have a byte array that can be used exactly as the original literal.
With it I can do things like
sizeof(myConst)
and get the same result as withsizeof("Hello")
. If I change string to a pointer, sizeof would return the size of a pointer, not the size of the string...... and obviously when doing things this way changing the pointer becomes meaningless, as there is no pointer to change anymore.