C++ 中 const 声明之间的区别
之间有什么区别
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不同之处在于 for
You 指向一个不能更改的类,因为它是 const。
但是您可以修改 myClass 指针(让它指向另一个类;这对调用者没有任何副作用,因为它的指针被复制,它只会更改您本地的指针副本)
相比之下,
现在myClass指向一个可以修改的类,但不能更改参数。
The difference is that for
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
Now myClass points to a class that can be modified while you cannot change the parameter.
在第一个中,您声明一个接受指向常量 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.
经验法则是从右到左阅读声明:
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诀窍是向后阅读这些内容:
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.
正如其他答案中提到的,这个定义意味着参数
myClass
指向一个不能修改的Class
实例(mutable
和>const_cast
除外)由函数执行。 但是,函数体中的myClass
变量可以更改为指向Class
的不同实例。 这是该函数的实现细节。另一方面,这个定义意味着
myClass
参数是一个指向Class
实例的指针,该实例不是 const,因此可以被函数用来完全操作类实例,但是myClass
指针变量本身不能更改为指向函数体中的任何其他内容。其他答案尚未提出的一个重要观点是,对于函数签名,在考虑函数类型时,任何顶级 const 或 易失性限定都将被忽略。 这是因为参数总是按值传递,因此它们是否为 const 仅影响参数本身是否可以在函数体内更改,而不会影响调用者。
因此这两个函数声明是等价的。
As mentioned in other answers, this definition means that the parameter
myClass
points to an instance ofClass
that may not be modified (mutable
andconst_cast
excepted) by the function. However themyClass
variable in the function body could be change to point at a different instance ofClass
. This is an implementation detail of the function.On the other hand this definition means that the
myClass
parameter is a pointer to aClass
instance that is not const and hence can be used by the function to fully manipulate the class instance, but that themyClass
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.
在 C++ 中,this
和 this
都意味着
ptr
是一个变量指针,指向MyClass
类型的常量对象。 也就是说,您无法通过ptr
更改该对象。 但是,您可以使ptr
本身指向MyClass
的某个其他对象。相反,这
意味着
ptr
是一个指向变量MyClass
对象的常量指针。 在这里,您确实可以更改 ptr 指向的对象,但不能使 ptr 指向其他对象。请注意,在上述三种语法中,第二种语法有点奇怪,但它是有效的语法。 它不遵循这里其他人建议的从左到右的阅读规则。 不过,这就是您的 C++ 生活。
In C++ this
and this
both mean that
ptr
is a variable pointer that points to a constant object of typeMyClass
. That is, you can't change the said object throughptr
. However, you can makeptr
itself point some other object ofMyClass
.In contrast, this
implies
ptr
is a constant pointer pointing to a variableMyClass
object. Here you can indeed change the object that ptr is pointing to, but you cannot makeptr
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.