C++ 新手。 关于常量指针的问题

发布于 2024-07-26 10:36:31 字数 315 浏览 5 评论 0原文

我正在尝试通过一些网络教程来学习 C++。 我没有可用的编译器,否则我会尝试一下。 我不确定 const 指针是什么意思。 这是否意味着它总是指向相同的内存地址? 你为什么要这么做? 下面的代码合法吗?

...
int * const aPointer = new int;
... //do something with aPointer
delete aPointer;
... //do something else, including possibly more 'new' statements
aPointer = new int;
...

I am trying to learn C++ via some web tutorials. I don't have a compiler available to me, otherwise I would try this out. I'm not sure what is meant by a const pointer. Does that just mean it always points to the same memory address? Why would you ever want to do that? Would the following code be legal?

...
int * const aPointer = new int;
... //do something with aPointer
delete aPointer;
... //do something else, including possibly more 'new' statements
aPointer = new int;
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

烟凡古楼 2024-08-02 10:36:31

记住 const 如何与指针一起工作的一个简单方法是记住它总是适用于它左边的任何内容,除非它是最左边的关键字,在这种情况下它适用于右边。

示例:

指向常量字符的指针:
指针可以更改为指向其他内容,但它最初指向的 char 不能更改值。

const char * p;

指向字符的常量指针:
指针不能更改为指向其他任何内容,但它指向的 char 可以更改值。

char *const p;

指向常量字符的常量指针:
指针不能更改为指向其他任何内容,并且它指向的 char 也不能更改值。

const char *const p;

A simple way to remember how const works with pointers is to remember that it always applies to whatever is to the left of it, unless it's the left-most keyword, in which case it applies to the right.

Examples:

Pointer to a constant char:
The pointer can be changed to point to something else, but the char it initally points to cannot change value.

const char * p;

Constant pointer to a char:
The pointer cannot be changed to point to anything else, but the char it points to can change value.

char *const p;

Constant pointer to a constant char:
The pointer cannot be changed to point to anything else, and the char it points to cannot change value.

const char *const p;
葵雨 2024-08-02 10:36:31

常量指针可能有几种不同的含义。 我建议查看 C++ FAQ Lite就此事。

您可以:

const int* p;
int* const p;
const int* const p;

这三个含义不同。 是的,这有点令人困惑。

在你的例子中,你有第二个,这意味着你有一个指向非常量对象的常量指针。 也就是说,您可以通过指针更改整数的值,但无法更改指针指向的内容。 所以你发布的代码是不合法的。

Const pointer could mean a few different things. I recommend checking out the C++ FAQ Lite on the matter.

You can have:

const int* p;
int* const p;
const int* const p;

All three mean different things. Yes, it's kind of confusing.

In your case, you have the second, which means you have a constant pointer to a non-constant object. That is, you can change the value of the integer via the pointer, but you can't change what the pointer points to. So the code you posted would not be legal.

路还长,别太狂 2024-08-02 10:36:31

仅靠读书是不可能学会开车的。

如果您想学习 C++,请为自己准备一个 C++ 编译器。 g++ 以及 Visual Studio 2008 Express Edition 都是免费的。

至于你的问题,常量指针是一个仅准备就绪的内存区域。 示例:类可以提供对内部缓冲区的只读访问。

请注意,您还有一个 const 指针,它也是 const,也就是说,

const char * const p 

在这种情况下,甚至无法修改指针的值。

You can't learn to drive a car just by reading books.

Get yourself a C++ compiler if you want to learn C++. g++ is free of charge, as well as Visual Studio 2008 Express Edition.

As for your question, a const pointer is a zone of memory that is ready only. Example: A class may provide read-only access to an internal buffer.

Note that you also have the const pointer that is also const, aka

const char * const p 

In that case, even the value of the pointer cannot be modified.

a√萤火虫的光℡ 2024-08-02 10:36:31

天啊,

要轻松记住这一点,您可以使用 Scott Meyers 在他的优秀著作《Effective C++》(已清理的亚马逊链接

您在星号所在的声明中画一条线。

  • 如果关键字 const 出现在该行的左侧,则您无法更改所指向的项目的值。
  • 如果关键字 const 出现在该行的右侧,则无法将指针更改为指向另一个位置。
  • 如果 const 出现在两边,那么你就不能改变指针,也不能改变你所指向的值。

顺便说一句,那本书非常棒,虽然不适合初学者,但绝对是让您的 C++ 知识更上一层楼的方法! 强烈推荐。

HTH

欢呼,

G'day,

To remember this easily you can use the trick that Scott Meyers describes in his excellent book "Effective C++" (sanitised Amazon link)

You draw a line through the declaration where the asterisk is located.

  • If the keyword const appears to the left of the line, then you can't change the value of the item that you're pointing to.
  • If the keyword const appears to the right of the line, then you can't change the pointer to point to another location.
  • If const appears on both sides, then you can't change the pointer and you can't change the value of what you're pointing to.

BTW That book is excellent, and while not for a beginner, is definitely a way of taking your C++ knowledge to the next level! Highly recommended.

HTH

cheers,

叹沉浮 2024-08-02 10:36:31

const 指针意味着您可以更改所指向变量的值,但不能更改指针所指向的位置。 我自己并不经常使用它们,但 const 指针的一个常见应用是定义需要寻址的特定内存段。 请参阅此问题了解更多信息。

顺便说一句,如果可以的话,您应该尝试在计算机上安装编译器。 我已经多次向自己证明,人脑是糟糕的 C++ 编译器。

A const pointer means that you can change the value of the variable which is being pointed to, but you can't change where the pointer is pointed. I don't use them often myself, but a common application for const pointers is in defining specific memory segments that you need to address. See this question for more information.

As an aside, you should try to get a compiler on your computer if you can. I've shown myself many times that human brains are poor C++ compilers.

别低头,皇冠会掉 2024-08-02 10:36:31

您的代码不合法​​。 如果 aPointer 被这样声明为 const,则不能分配给 aPointer (除非使用复制样式初始化,实际上这不是赋值,尽管它看起来像它)。

但通常当人们说“const 指针”时,他们指的是 const int * aPointer,而不是代码中的 int * const aPointer。 整个互联网都会立即解释其中的差异。 据我所知,标准中没有定义术语“const 指针”,因此我们可以自由地这样做,尽管它可能会令人困惑。 “指向 const 的指针”将是一个明确的描述,并且指向 const 的指针比本身为 const 的指针更常用。

本身是常量的指针用于引用某些事物,您不希望指针在其生命周期中的任何时候引用不同的事物。 例如,this 是一个本身就是常量的指针,因为“this 对象”通过执行成员函数仍然保持同一个对象。 C++ 语言选择不让您决定是否要为 this 分配其他值,以使其引用其他对象。 在 C++ 中,引用也经常用于此目的,因为它们不能“重新定位”以更改引用对象。

Your code is not legal. You can't assign to aPointer (except using copy-style initialisation, which in fact is not assignment even though it looks like it) if aPointer is declared const like that.

But usually when people say "a const pointer", they mean const int * aPointer, not int * const aPointer as you have in your code. The whole internet will explain the difference at the drop of a hat. As far as I know, the term "const pointer" isn't defined in the standard, so we're free to do this even though it's potentially confusing. "Pointer-to-const" would be an unambiguous description, and a pointer-to-const is much more commonly used than a pointer-which-is-itself-const.

A pointer-which-is-itself-const is used to refer to something, where you won't want the pointer to refer to a different thing at any point in its life. For instance, this is a pointer-which-is-itself-const, because "this object" remains the same object through the execution of a member function. The C++ language has opted not to let you decide part way through that you want to assign some other value to this, to make it refer to some other object. In C++ references often serve that purpose too, since they cannot be "re-seated" to change the referand.

梦醒时光 2024-08-02 10:36:31

在您的代码中,指针无法移动,但指向的数据可以更改。

在第一次删除之前都是合法的。 随后的 new 将不起作用,因为它是对常量的赋值。

这种情况比较少见,更常见的是看到数据指向的位置不可更改但指针可以移动。

int bar;
int baz;
const int * foo = &bar;
*foo = 4; // illegal
foo = &baz; // legal

指针和值都为 const 在字符串中很常见

const wchar_t * const myString = L"String that will never change.";

In your code, the pointer cannot move, but the data pointed to can change.

It's legal up to the first delete. A subsequent new would not work because it's an assignment to a constant.

It's relatively unusual to see this, more common is to see where the data pointed to is unchangeable but the pointer can move.

int bar;
int baz;
const int * foo = &bar;
*foo = 4; // illegal
foo = &baz; // legal

having both pointer and value being const is common with strings

const wchar_t * const myString = L"String that will never change.";
够钟 2024-08-02 10:36:31

由于您是 C++ 新手,因此要了解此问题以及您可能有或不知道的许多其他问题的答案,请查看 C++ 常见问题解答

Since you're new to C++, for the answer to this question and many other questions you may have or don't know you have, check out the C++ FAQ

北座城市 2024-08-02 10:36:31

我没有可用的编译器,否则我会尝试一下

每个人都有一个可用的 C++ 编译器:http://www.online-compiler.com

还有很多其他的,但这似乎有效......

I don't have a compiler available to me, otherwise I would try this out

Everyone has a C++ Compiler available to them: http://www.online-compiler.com

There are many others, but this seems to work...

坏尐絯 2024-08-02 10:36:31

正如已经指出的,也许最常见的 const 指针是

const char* p;

变量 p 可以更改,但 p 指向的数据是不可修改的。

然而,将 const 关键字移到星号的左侧并不会改变声明的含义:

char const* p;

我更喜欢后者,因为在声明指向 const 指针的 const 指针时,更容易记住 const 关键字的位置:

char const* const* p;

同样,变量p 可以改变,并且指向的数据是不可修改的。 此外,数据被声明为 const 指针,这意味着它指向无法修改的数据。

此类型更常见的表示法是

const char* const* p;

将 const 关键字紧邻其修改的星号(或用于引用的与号)的左侧,可以轻松创建涉及 const 关键字的复杂类型。 例如,指向 const 指针的指针:

char const** p;

和指向指针的 const 指针:

char* const* p;

请记住从右向左“读取”指针声明,并且不要在每个语句中声明多个指针,以避免出现很多混乱。

As it has already been pointed out the perhaps most common const pointer is

const char* p;

The variable p can change, but the data p points to is unmodifable.

However, moving the const keyword to the left of the asterisk does not alter the meaning of the declaration:

char const* p;

I prefer the later since it becomes much easier to remember where to place the const keywords when declaring const pointers to const pointers:

char const* const* p;

Again, the variable p can change and the data pointed to is unmodifiable. Furthermore, the data is declared as const pointers meaning that it points to data that cannot be modified.

The more common notation for this type is

const char* const* p;

Placing the const keyword immediately to the left of the asterisk it modifies (or ampersand for reference) makes it easy to create complex types involving the const keyword. For example, a pointer to const pointers:

char const** p;

and a const pointer to pointers:

char* const* p;

Remember to "read" pointer declarations from the right to the left, and don't declare more than one pointer in each statement to avoid a lot of confusion.

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