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

 
Alexey Kozitsyn:
What is called 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.

How can an object call a method? A method can be called from a function or from another method.

It is not just a class method that is called, but a method of a specific object. The class itself is an abstraction.

 
Dmitry Fedoseev:

How can an object call a method? A method can be called from a function or from another method.

It is not just a class method that is called, but a method of a specific object. The class itself is an abstraction.

That's what's so trivial. And a record of the type

obj.method();

What is it called?

You are writing it yourself:

It's not just a class method that is called, but a method of a specific object.

You can also say that it's acceptable:

method();

What does method call?

That is, what method is called by?

 
@Alexey Kozitsyn:

That's a tough one. And an entry like...

what do you call that?

You write it yourself:

You tell me it's allowed:

What does method call?

That is, what is the method used to call it?

Look at the situation. You have problems, not me. Alexey's example seems incorrect to you, while it is quite clear and understandable to me.

--

"A type entry" is called a call to the "method" method of the "obj" object.

--

"You'll also tell me that such a record is permissible" - the impression is that you're the one writing about such a thing.

---

The method does not call anything. The method is called.

 
Dmitry Fedoseev:

Look at the situation. It is you, not me, who is confused. Alexei's example seems incorrect to you, while it seems clear and understandable enough to me, it did not cause me any questions, but only gave me an understanding.

--

"A type entry" is called a call to the "method" method of the "obj" object.

--

"You'll also tell me that such a record is permissible" - the impression is that you're the one writing about such a thing.

---

The method doesn't call anything. The method is called.

Alexei's example is acceptable, but it should not, in my personal opinion, be an example for others.

You see, you still haven't answered my question: what initiates a method call of a class (or otherwise, what invokes a method (not what is invoked through a method, but what/who invokes the method itself))?

Any method. It's a simple question, you wrote an article for beginners on OOP.

A method doesn't call anything. The method is called.

So what is it called with?
 
Alexey Kozitsyn:

1. Alexei's example is acceptable, but he should not, in my personal opinion, be an example to others.

2. You see, you still haven't answered my question: what initializes a class method call (or otherwise, what invokes a method (not what is invoked via a method, but what/who invokes the method itself))?

3. any method. It's a simple question, you wrote an article for beginners on OOP.

1. Suggest your own example.

2. No answer. Because I don't know what we're talking about.

3. I don't know what we're talking about either.

 
void fun(){
   obj.method();
}
The "method" method of the "obj" object is called from the "fun" function.
 
Dmitry Fedoseev:

1. Offer your own example.

2. No answer. Because I don't know what we're talking about.

3. I don't know what we're talking about either.

2. The discussion is about what calls the class method (not the static method). The method of the class is called by the object of the class or otherwise, the object of the class initiates the call of the method of the class, e.g. this:

obj.method();   // obj - объект класса А, method() - метод класса А

If the method is constant, it cannot modify the object through which it was called, i.e. the obj object. An object passed to it (e.g. obj2), on the other hand:

obj.method( obj2 );

can be modified if passed by reference without a const modifier. That's all.

I initially did not understand the statements of forum "corefeys":

Forum on trading, automated trading systems and testing trading strategies

What does const modifier mean after method declaration

Combinator, 2016.01.31 20:32

In C++ it means that the method cannot change class members, except for members declared mutable.

Since in MQL you can't get such a thing, it means that the method cannot change the object in principle.

Or

Forum on Trading, Automated Trading Systems and Strategy Tests

What does const modifier after the method declaration mean?

Vasiliy Sokolov, 2016.02.01 11:55

A constant method is another example of the proverb "we tried to do better, but it turned out to be the same as always". I think this is a motto for C++ at all. There is no practical use, but it significantly complicates designing of OOP-programs, because you constantly need to control the type of modified object (it must be constant as well).

or

Forum on trading, automated trading systems and testing trading strategies

What does const modifier mean after method declaration

Vasiliy Sokolov, 2016.02.01 14:02

Example Alexey wrote. A constant method cannot change members of its class...

Well, why can't it do that? Everything changes perfectly well. And the wording itself is incorrect: a constant method cannot change the members of an instance of its class.

 
Vasiliy Sokolov:

I'm not denying it. Sharpe is indeed a perfect, great and beautiful language.

OK, let's say you can always get away with const. But you'd better briefly explain with an example, what gives const? What is its real use? By example, and preferably not written out of thin air.

It is one thing when you write a project for yourself and the whole project in thousands - tens of thousands strings. It's another thing when you give a project of millions of lines to a client and a Hindu Harikrishna is sitting there with no clue. I worked with Hindus from Bangalore, and the level was actually lower than that of our guys.

It's like there: they train as programmers and take the rest of their relatives to work, and they might not know anything at all. They are gypsies.

And if the critical code is packed into a libu, there is less chance of corruption. That's what I think.

 
Alexey Kozitsyn:

...

Why can't it?! Everything can be changed just fine. And the wording itself is incorrect: a constant method cannot change members of an instance of its class.

This is what I mean because we are not speaking of the type in this context. "Own class" is obviously its own instance (i.e. object).

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

The_x member belongs to the same class as the bar method.

C obj._x - here the _x member is in a foreign class obj.

It seems acceptable to me to talk about a class rather than an instance, because it's obvious that _x belongs to our class, which we write, and obj.x to the foreign class, which we handle.

Speaking of OOP terminology, all the terminology here is a bit of that (or even a lot of that).

 

Personally, I always understood const-methods as methods which cannot change class variables.

I've used them very rarely, because I've often encountered a situation where a method redefinition requires to change something, while the base method is constant (it was already mentioned here).

Static methods are used extensively. In almost any complex class. Usually these are "serving" additional methods that do not require access to the class variables.

In addition, for constants like "number of seconds in a day" I try to use a static const const, rather than #define. In this case, declaration and initialization of such constant are in different places, but the type control occurs and it helps me out more than once.

Reason: