Difference between const declarations in C++

眉间皱痕 提交于 2019-12-17 18:47:11

问题


What is the difference between

void func(const Class *myClass)

and

void func(Class *const myClass)

See also:

  • C++ const question
  • How many and which are the uses of "const" in C++?

and probably others...


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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);



回答6:


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.



来源:https://stackoverflow.com/questions/487048/difference-between-const-declarations-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!