V
V
Vladimir A2020-12-12 12:39:59
C++ / C#
Vladimir A, 2020-12-12 12:39:59

What is the name of the parameter that comes after the method?

For example, a certain method in a class:

void dosmth(uint8_t value) MYPARAM{
}


I understand that you can put const instead of MYPARAM, for example, why is it used?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuriy Vorobyov, 2020-12-13
@YuriyVorobyov1333

This stems from the requirement of the language and the compiler, you can write a function inside a class:

void PrintClassName(const ClassName& cl) {
cout << cl.Get() << endl;
}

It would seem that the code is only engaged in printing, it takes a parameter by a constant reference, but when the code is run, an error will come out, and this is because nowhere in the PrintClassName method it is explicitly stated that it does not change the object. The compiler does not allow calling those methods for which it is not explicitly indicated that they do not change the object, it is enough to rewrite the code like this:
void PrintClassName(const ClassName& cl) const {
cout << cl.Get() << endl;
}

And everything will work.
Or we can consider another option, and substitute the volatile keyword in some function, which says that the value can change from outside.
In fact, everything that can be put in place of MYPARAM is a pointer for the compiler, which induces or vice versa does not induce it to do something.

P
Pavel, 2020-12-17
@HEKOT

This keyword is const

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question