C++ 中 const 声明之间的区别

发布于 2024-07-11 23:38:00 字数 423 浏览 6 评论 0原文

之间有什么区别

void func(const Class *myClass)

void func(Class *const myClass)

另请参阅:

大概其他的...

What is the difference between

void func(const Class *myClass)

and

void func(Class *const myClass)

See also:

and probably others...

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

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

发布评论

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

评论(6

作妖 2024-07-18 23:38:00

不同之处在于 for

void func(const Class *myClass)

You 指向一个不能更改的类,因为它是 const。
但是您可以修改 myClass 指针(让它指向另一个类;这对调用者没有任何副作用,因为它的指针被复制,它只会更改您本地的指针副本)
相比之下,

void func(Class *const myClass)

现在myClass指向一个可以修改的类,但不能更改参数。

The difference is that for

void func(const Class *myClass)

You point to a class that you cannot change because it is const.
But you can modify the myClass pointer (let it point to another class; this don't have any side effects to the caller because it's pointer is copied, it only changes your local the pointer copy)
In contrast

void func(Class *const myClass)

Now myClass points to a class that can be modified while you cannot change the parameter.

百善笑为先 2024-07-18 23:38:00

在第一个中,您声明一个接受指向常量 Class 对象的指针的函数。 您无法修改函数内的对象。
在第二个函数中,您声明一个函数,该函数接受指向非常量 Class 对象的常量指针。 可以通过指针修改对象,但不能修改指针值本身。

我始终牢记这个简单的规则:const 始终适用于紧邻其左侧的事物,如果该事物不存在,则适用于紧邻右侧的事物。

另请查看这个 我一周前提出的问题,它指出了一些非常有用的链接来理解 const 的正确性。

In the first one you're declaring a function that accepts a pointer to a constant Class object. You cannot modify the object inside the function.
In the second one you're declaring a function that accepts a constant pointer to a non constant Class object. You can modify the object through the pointer, but cannot modify the pointer value itself.

I always keep in mind this easy rule: const always applies on the thing at the immediate left of it, if this thing doesn't exists, it applies to the thing on the immediate right.

Also take a look to this question which I asked a week ago, it points to some very useful links to understand const correctness.

习惯成性 2024-07-18 23:38:00

经验法则是从右到左阅读声明:

void func(const Class *myClass) 是一个指向 const 类的指针(或者严格来说“指向 const 类的指针”)

void func(Class *const myClass) 是指向 Class 的 const 指针

A rule of thumb is to read the declarations right to left:

void func(const Class *myClass) is a pointer to a const Class (or strictly speaking "a pointer to a Class which is const")

void func(Class *const myClass) is a const pointer to a Class

离不开的别离 2024-07-18 23:38:00

诀窍是向后阅读这些内容:


void func(const Class *myClass)

读取“myClass 是指向 const 类的指针”,这意味着我无法在类中进行更改


void func(类 *const myClass)
读取

“myClass 是一个指向类的 const 指针”,这意味着我无法更改该指针。

The trick is to read theese things backwards:


void func(const Class *myClass)

Reads "myClass is a pointer to a Class that is const" wich means I can't make changes in Class


void func(Class *const myClass)

Reads "myClass is a const pointer to a Class" wich means I can't change the pointer.

冷情妓 2024-07-18 23:38:00
void func(const Class *myClass) { //...

正如其他答案中提到的,这个定义意味着参数 myClass 指向一个不能修改的 Class 实例(mutable>const_cast 除外)由函数执行。 但是,函数体中的 myClass 变量可以更改为指向 Class 的不同实例。 这是该函数的实现细节。

void func(Class *const myClass) { // ...

另一方面,这个定义意味着 myClass 参数是一个指向 Class 实例的指针,该实例不是 const,因此可以被函数用来完全操作类实例,但是 myClass 指针变量本身不能更改为指向函数体中的任何其他内容。

其他答案尚未提出的一个重要观点是,对于函数签名,在考虑函数类型时,任何顶级 const 或 易失性限定都将被忽略。 这是因为参数总是按值传递,因此它们是否为 const 仅影响参数本身是否可以在函数体内更改,而不会影响调用者。

因此这两个函数声明是等价的。

void func(Class *const myClass);

void func(Class *myClass);
void func(const Class *myClass) { //...

As mentioned in other answers, this definition means that the parameter myClass points to an instance of Class that may not be modified (mutable and const_cast excepted) by the function. However the myClass variable in the function body could be change to point at a different instance of Class. This is an implementation detail of the function.

void func(Class *const myClass) { // ...

On the other hand this definition means that the myClass parameter is a pointer to a Class instance that is not const and hence can be used by the function to fully manipulate the class instance, but that the myClass pointer variable itself cannot be altered to point at anything else in the function body.

One important point that hasn't been raised by other answers is that for function signatures, any top level const or volatile qualification is disregarded when considering the type of the function. This is because parameters are always passed by value, so whether they are const or not only affects whether the parameter itself can be changed in the body of the function and cannot affect the caller.

Thus these two function declarations are equivalent.

void func(Class *const myClass);

void func(Class *myClass);
电影里的梦 2024-07-18 23:38:00

在 C++ 中,this

const MyClass *ptr 

和 this

MyClass const *ptr

都意味着 ptr 是一个变量指针,指向 MyClass 类型的常量对象。 也就是说,您无法通过ptr更改该对象。 但是,您可以使 ptr 本身指向 MyClass 的某个其他对象。

相反,这

MyClass *const ptr

意味着ptr是一个指向变量MyClass对象的常量指针。 在这里,您确实可以更改 ptr 指向的对象,但不能使 ptr 指向其他对象。

请注意,在上述三种语法中,第二种语法有点奇怪,但它是有效的语法。 它不遵循这里其他人建议的从左到右的阅读规则。 不过,这就是您的 C++ 生活。

In C++ this

const MyClass *ptr 

and this

MyClass const *ptr

both mean that ptr is a variable pointer that points to a constant object of type MyClass. That is, you can't change the said object through ptr. However, you can make ptr itself point some other object of MyClass.

In contrast, this

MyClass *const ptr

implies ptr is a constant pointer pointing to a variable MyClass object. Here you can indeed change the object that ptr is pointing to, but you cannot make ptr to point to some other object.

Note that among the above three kinds of syntax, the second one is a bit odd, but it is valid syntax. It doesn't follow the left to right reading rule that other folks here have suggest. But then, that's life in C++ for you.

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