简体   繁体   English

C ++中const声明的区别

[英]Difference between const declarations in C++

What is the difference between 有什么区别

void func(const Class *myClass)

and

void func(Class *const myClass)

See also: 也可以看看:

and probably others... 可能还有其他人......

The difference is that for 区别在于

void func(const Class *myClass)

You point to a class that you cannot change because it is const. 你指向一个你不能改变的类,因为它是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 但你可以修改myClass指针(让它指向另一个类;这对调用者没有任何副作用,因为它的指针被复制,它只会改变你的本地指针副本)

void func(Class *const myClass)

Now myClass points to a class that can be modified while you cannot change the parameter. 现在,myClass指向一个可以在您无法更改参数时进行修改的类。

In the first one you're declaring a function that accepts a pointer to a constant Class object. 在第一个中,您声明了一个接受指向常量Class对象的指针的函数。 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. 在第二个中,您将声明一个接受指向非常量Class对象的常量指针的函数。 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. 我总是记住这个简单的规则: const总是适用于它左边的东西,如果这个东西不存在,它适用于右边的东西。

Also take a look to this question which I asked a week ago, it points to some very useful links to understand const correctness. 另外看一下我一周前问过的这个问题,它指出了一些非常有用的链接来理解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(const Class *myClass)是一个指向const类的指针(或严格来说是“指向一个const的类的指针”)

void func(Class *const myClass) is a const pointer to a Class void func(Class *const myClass)是一个指向Class的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 读取“myClass是指向const的类的指针”,这意味着我无法在Class中进行更改

void func(Class *const myClass)

Reads "myClass is a const pointer to a Class" wich means I can't change the pointer. 读取“myClass是指向类的const指针”,这意味着我无法更改指针。

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. 正如其他答案中所提到的,这个定义意味着参数myClass指向一个Class的实例,该实例可能不被函数修改( mutableconst_cast除外)。 However the myClass variable in the function body could be change to point at a different instance of Class . 但是,函数体中的myClass变量可以更改为指向不同的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. 另一方面,这个定义意味着myClass参数是一个指向不是const的Class实例的指针,因此可以被函数用来完全操作类实例,但是myClass指针变量本身不能改为指向函数体中的其他任何东西。

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. 其他答案未提出的一个重要问题是,对于函数签名,在考虑函数的类型时,忽略任何顶级const或volatile限定。 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);

In C++ this 在C ++中

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 . 两者都意味着ptr是一个指向MyClass类型的常量对象的变量指针。 That is, you can't change the said object through ptr . 也就是说,你不能通过ptr改变所述对象。 However, you can make ptr itself point some other object of MyClass . 但是,你可以让ptr本身指向MyClass其他一些对象。

In contrast, this 相比之下,这个

MyClass *const ptr

implies ptr is a constant pointer pointing to a variable MyClass object. 暗示ptr是一个指向变量 MyClass对象的常量指针。 Here you can indeed change the object that ptr is pointing to, but you cannot make ptr to point to some other object. 在这里你确实可以改变ptr指向的对象,但是你不能让ptr指向其他对象。

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. 但是,那就是C ++的生活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM