c++最终常量字符串

发布于 2024-10-06 09:23:22 字数 352 浏览 4 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

み格子的夏天 2024-10-13 09:23:22
const std::string myConst("Hello");
const std::string myConst("Hello");
独自←快乐 2024-10-13 09:23:22

是的,const char* const 是声明您不会更改的 C 样式字符串的正确方法。

或者更好:

#include <string>
const std::string myConst = "Hello";

Yes, const char* const is the correct way to declare a C-style string which you will not change.

Or better:

#include <string>
const std::string myConst = "Hello";
美人如玉 2024-10-13 09:23:22
const char * myConst = "Hello";

这意味着所指向的对象不能改变。

char * const myConst = "Hello";

这意味着指针指向的位置不能更改,但对象的值可以更改。

const char * const myConst = "Hello";

这意味着两者都不会改变。根据我的经验,没有人记得这一点,但它总是可以在网上找到!

典型的是,在我写我的文章时三个人回答!

const char * myConst = "Hello";

This means the object pointed at cannot change.

char * const myConst = "Hello";

This means the location pointed at by the pointer cannot change, but the object's value can.

const char * const myConst = "Hello";

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!

瞎闹 2024-10-13 09:23:22

我不太明白你的问题。为了或多或少地模仿 final Java 关键字,它可以是

const char * const myConst = "Hello";

(如果指针不会改变),和/或:

const char * myConst = "Hello";

如果指针之后可能会改变。最后,请注意,在第一个版本中,您实际上无法更改指针本身,因为它是常量。

I don't exactly understand your question. To more or less mimic the final Java keyword, it would be either

const char * const myConst = "Hello";

(if the pointer is not going to change), and/or:

const char * myConst = "Hello";

if the pointer may change afterwards. Finally, note that with the first version, you cannot actually change the pointer itself, because it is constant.

拧巴小姐 2024-10-13 09:23:22

根据迭戈的编辑,是的,这是正确的书写方式。人们通常不会声明变量 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.

庆幸我还是我 2024-10-13 09:23:22

从技术上讲,

char * const myConst = "Hello";

这是最正确的答案,因为重新分配指针会给您留下一个可能无法恢复的字符串。

某些实现允许您更改“Hello”中的字符(即使这是一个坏主意),因此中的第一个 const

char const * const myConst = "Hello";

是个好主意。个人认为

char const * myConst = ...;


const char * myCount = ...;

是等价的,我倾向于强制执行一种风格指南,即 const 始终遵循它所修改的项目。从长远来看,它有时会减少误解。

也就是说,大多数人不知道如何在 C++ 中正确使用 const,因此它要么使用得不好,要么根本不使用。那是因为他们只是使用 C++ 作为改进的 C 编译器。

Technically,

char * const myConst = "Hello";

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

char const * const myConst = "Hello";

Is a great idea. Personally as

char const * myConst = ...;

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.

黯然#的苍凉 2024-10-13 09:23:22

但是,实际上,你可以改变什么
该指针指向。那么,为什么要
人们没有将指针声明为
也恒定吗?什么是正确的
怎么办?

因为它很少有用,所以您通常希望堆栈上的值(包括指针)是非常量的。得到萨特& Alexandrescu的《编码标准》一书中,就解释了这一点。

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?

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.

快乐很简单 2024-10-13 09:23:22

真正的要点是,程序员被迫将一些指针至少声明为 const char * 来存储双引号之间的 char 数组。没有什么强迫他们(没有编译器警告或错误)也使指针恒定......因为人们很懒,你可以得出自己的结论。他们甚至可能并没有真正尝试定义一个常量,他们只是想关闭编译器错误(好吧,在本例中是警告)。

记住这一点,无论如何我可能会寻求不同的解决方案:

const char myConst[] = "Hello";

这里的区别在于,这样我不会将用作字符串的原始字节数组衰减为指针,我仍然会有一个可以准确使用的字节数组作为原始文字。

有了它,我可以执行诸如 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:

const char myConst[] = "Hello";

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 with sizeof("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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文