简体   繁体   中英

'std::list<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::list<_Ty>' to 'std::list<_Ty> &'

I'm getting this error message when try to compile the following

void MyClass::MyFunc() const
{
      int i= 0;
      myList.push_back(i);
}

I've already had to add the const to the function, despite it returning void , to fix an identical problem with the invocation of the function itself, but now that it's occurring with a member of the STL, I'm thinking something is very wrong with my code. For reference, the error was

cannot convert 'this' pointer from 'const MyClass' to 'MyClass &'

I actually have very little idea of what's going on since I inherited this from someone with a very abusive relationship with coding standards and best practices. If someone could tell me what problem this is a symptom of and what I should be looking for in the code, it would help me a lot.

如果myList是的数据成员MyClass ,具有push_back()要修改的myList的数据成员(因此要修改的实例MyClass指向通过this ),因此MyFunc()方法不能被标记为const

const on a method means that it can't modify the instance it is called on, including any instance members.

You need to take a look at the code base and distinguish between methods that can modify the class instance (which should not be const ) and those that just provide information about the class instance (which should be const ).

$9.3.1/3: When an id-expression (5.1) that is not part of a class member access syntax (5.2.5) and not used to form a pointer to member (5.3.1) is used in a member of class X in a context where this can be used (5.1.1), if name lookup (3.4) resolves the name in the id-expression to a non-static non-type member of some class C, and if either the id-expression is potentially evaluated or C is X or a base class of X, the id-expression is transformed into a class member access expression (5.2.5) using (*this) (9.3.2) as the postfix-expression to the left of the . operator.

What this means is that the expression

myList.push_back(i);

is treated as

(*this).myList.push_back(i);

$9.3.1/4: A non-static member function may be declared const, volatile, or const volatile. These cv-qualifiers affect the type of the this pointer (9.3.2).

$9.3.2/1: In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

In summary, what this means is that type of 'this' in your member function which is 'const' is 'MyClass const *'. Effectively, it means that Myclass::MyFunc is not allowed to modify the state of the object pointed to by the 'this' pointer.

Assuming myList is of type std::list, the method std::list::push_back modifies the instance variable 'myList' as it inserts the value of 'i' in the list.

But this breaks the semantics of the 'const' member function MyClass::MyFunc which promises that it won't modify the state of the object.

So, you really need to remove the 'const' from this member function or use const_cast to remove the constness for this specific operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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