简体   繁体   English

C ++这个指针总是const

[英]C++ the this pointer is always const

For a non const member function of class X, this pointer is of type X* const. 对于类X的非const成员函数,此指针的类型为X * const。

Then, this pointer for a member function is always const. 然后,成员函数的这个指针总是const。

Then, do we always need to const cast as in: 然后,我们总是需要const cast,如:

void foo::p() {                        
      const_cast <int&> (member) = 1;    
}

Am I missing something here ? 我在这里错过了什么吗?

For a non const member function of class X, this pointer is of type X* const. 对于类X的非const成员函数,此指针的类型为X * const。

No, the type of the this pointer inside a non-const member function is just X* (and it is an rvalue). 不,非const成员函数中this指针的类型只是X* (它是一个rvalue)。 But even if this were X* const , that would still not prevent you from changing data members. 但即使thisX* const ,仍然不会阻止您更改数据成员。

Your code example doesn't match your question. 您的代码示例与您的问题不符。 It shows a const member function. 它显示了一个const成员函数。 Inside a const member function, the type of this is const X* . 里面一个const成员函数的类型, thisconst X* You cannot change any data members inside a const member function (with the exception of data members declared as mutable ). 您不能更改const成员函数内的任何数据成员(声明为mutable的数据成员除外)。 It is a contradiction to declare a member function as const if your goal is to change data members. 如果您的目标是更改数据成员,则将成员函数声明为const是一个矛盾。 Make up your mind. 下定决心。

For a non const member function of class X, this pointer is of type X* const. 对于类X的非const成员函数,此指针的类型为X * const。

Then, this pointer for a member function is always const. 然后,成员函数的这个指针总是const。

More or less, it's an rvalue, but can seen as a const pointer, but note: The pointer is const, but not the pointee. 或多或少,它是一个rvalue,但可以看作是一个const指针,但请注意: 指针是const,但不是指针。 That means, you cannot change which object this points to, but you can change the contents of the object. 这意味着,您无法更改此指向的对象,但您可以更改对象的内容。

In your example, the function is declared const: 在您的示例中,该函数声明为const:

void foo::p() const { 
              // ^---- here!

So in this case, this is of type X const * const (it's exactly an rvalue of type X const* but that does not matter here) - ie the pointer and the pointee are const. 所以在这种情况下, this是类型X const * const (它恰好是X const*类型的rvalue,但这里没关系) - 即指针和指针是const。 The latter is what really matters. 后者才是真正重要的。

So if you want to modify members, just don't use const for the method: 因此,如果要修改成员,只需不要使用const作为方法:

void foo::p() {    
           //^------ no const!
      member = 1;       // perfectly legal, because *this is not const
      this->member = 1; // the same
      this = new foo;   // still illegal, this cannot be assigned to
}

the const is after the star symbol, meaning "X* const", and not "const X* or X const*" (which are the same). const在星号后面,意思是“X * const”,而不是“const X *或X const *”(它们是相同的)。 therefore it means you cannot make "this" point to different object than the one he is pointing at, but you can change the object that is being pointed, which makes sense. 因此,这意味着你不能将“this”指向不同于他指向的对象,但你可以改变被指向的对象,这是有道理的。 "const X* or X const*" means you cannot change the object that is being pointed at the moment, but you can make the pointer point to different objects. “const X *或X const *”表示您无法更改此刻指向的对象,但可以使指针指向不同的对象。 hope it helps(: 希望能帮助到你(:

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

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