What the const modifier means after a method declaration - page 4

 
Dmitry Fedoseev:
It's not your own, but a totally foreign one with the same type.

What's not yours? That the type is the same?

Are you saying that a constant method cannot change someone else's class?

And when you're replying, please be more specific, I didn't understand what you're trying to say.

 
Alexey Kozitsyn:

What's not yours? That the type is the same?

Are you saying that a constant method cannot change someone else's class?

And when replying, please be more explicit, I did not understand what you mean.

It's you who's writing incomprehensibly Can change someone else's class of any type and of the same type as itself.
 

To put it simply. In a constant method, it's impossible to change values of variables declared in the same class where the constant method is located (or declared in its parent). Is it so?

 
Dmitry Fedoseev:
You're the one who doesn't understand what you're writing Can change someone else's class of any type and the same as itself.

Maybe you should watch what you're writing after all. How can a method change a class? Maybe a class object?

And the question was in the statement:

A constant method cannot change its class members.

Alexey gave an example that it can change, so I don't think the statement is correct, I wanted to hear Vasiliy's comment.

 
Alexey Kozitsyn:

Maybe you should watch what you're writing after all. How can a method change a class? Maybe a class object?

And the question was in the statement:

Alexey gave an example that it can change, so I think the statement is wrong, I wanted to hear Vasily's comment.

How about you read more thoughtfully? -"change the values of variables declared"

How can you talk about Alexey's assertion? He didn't just assert it, he confirmed it for real, gave an example, you can throw it into an editor and see when it compiles and when it doesn't.

 
Dmitry Fedoseev:

To put it simply. In a constant method, it's impossible to change values of variables declared in the same class where the constant method is located (or declared in its parent). Is it so?

No. You do know that each object of a class has its own set of variables declared in this class, right? And a constant method cannot change only the object (i.e. set) from which it was called.
 
Alexey Kozitsyn:

Maybe you should watch what you're writing after all. How can a method change a class? Maybe a class object?

..

Like this:

    void bar(X& obj) const 
    {
        obj._x = 42; // OK! obj передается по ссылке и не имеет модификатора const
}

 
Alexey Kozitsyn:
No. You do know that each object of a class has its own set of variables declared in that class, right? And a constant method cannot change only the object (i.e. set) from which it was called.
What does calling have to do with it? It's the location of the method and variables that matters. If the method and variables are located in the same class, it cannot be changed. If the class is passed by reference as a parameter, you can.
 
struct X
{

int _x, _y;
    
void foo() const 
    {
        _x = 42; // ERROR! метод объявлен, как const, не можем менять члены 
        _y = 42; // ERROR! 
    }

    void bar(Z& obj) const 
    {
        obj._x = 42; // OK! obj передается по ссылке и не имеет модификатора const
        _x = 42; // ERROR!
    }

    void bar(Z const& obj) 
    {
        obj._x = 42; // ERROR! obj передается по ссылке, как const
        obj._y = 42; // ERROR! obj передается по ссылке, как const
        _x = 42; // OK! метод объявлен без const на сонце
    }

   
};
That makes more sense.
 
Dmitry Fedoseev:
What does the call have to do with it? What matters is the location of the method and variables. If the method and variables are located in the same class, you cannot change. If the class is passed by reference as a parameter, you can.
The challenge here is that if object1 of class A calls a constant method of class A, then that method cannot change that object1 (i.e. the object1 that called it). If, however, object1 is passed by reference to the same constant method, but called from object2 of class A, then object1 can be changed. Object2, in turn, cannot.
Reason: