C ++ Difference Between Constant Positioning

I'm struggling to figure out the differences between the various places that you can put "const" in a function declaration in C ++.

What is the difference between a constant at the beginning:

const int MyClass::showName(string id){ ... } 

And const at the end like:

 int MyClass::showName(string id) const{ ... } 

In addition, that is the result of having const both at the beginning and at the end like this:

 const int MyClass::showName(string id) const{ ... } 
+6
source share
4 answers

const int MyClass::showName(string id) returns a const int object. Thus, the calling code cannot modify the returned int. If the calling code is like const int a = m.showName("id"); a = 10; const int a = m.showName("id"); a = 10; , then it will be flagged as a compiler error. However, as @David Heffernan noted below, since the integer is returned by the copy, the calling code is not required to use const int . It can very well declare int as a return type and change it. Since the object is returned by the copy, it makes no sense to declare the return type as const int .

int MyClass::showName(string id) const indicates that the showName method is a member function of const . A const member function is one that does not change any member variables of the class (unless they are marked mutable ). Therefore, if you have an int m_a member int m_a in the MyClass class, and if you try to execute m_a = 10; inside showName , you will get a compiler error.

Thirdly, this is a combination of the two above cases.

+18
source
  • const , bound to the return value, refers to the return value. Since the return value is copied, this is a meaningless declaration, and it does not matter whether it is included or not.
  • const after the parameter list means that the function does not change any state of an object that is not marked as mutable. This is a const member function, and if you have a const object, the compiler will not allow you to call non-constant member functions in the const object.

There is no interaction between these two uses of const - they are completely independent constructs

+10
source

The difference is that const is applied to various things.

This suggests that showName returns a constant value of int - immutable. Of course, since int returned by value, the presence of const does not play any role here.

 const int MyClass::showName(string id) 

And this suggests that showName does not change the observed state of MyClass (technically: it does not change any member that is not declared mutable ), and therefore you are allowed to call it by value of type const MyClass .

 int MyClass::showName(string id) const 

If you use both const , then both of the above apply.

+4
source

These kinds of questions tend to reinforce my determination to follow Dan Saks’s advice in this article, Conversations with the Guru: http://www.drdobbs.com/conversationsa-midsummer-nights-madness/184403835 .

In particular, it is about how const placement changes the situation. (*) I understand that I’m unlikely to translate someone from writing const int ... to int const ... , but this suggests that I prefer to do the latter.

(*), which states that replacing a constant with a type declaration at the very beginning is one change that has no effect.

This makes reading easier, because for any instance of the word const in the declaration, everything to the left of it is a type of what is a constant, and everything to the right is what is actually a constant.

Consider a declaration like:

 int const * * const pointerToPointer; 

The first const states that the integers at the end of the chain of pointers are constants and can be found in * * const pointerToPointer . Meanwhile, the second indicates that an object of type pointer to a pointer to const int is also const and that this object is pointerToPointer .

In the case of OP:

 int const MyClass::showName(string id){ ... } 

The const type is int, and const is the return value from the function.

Topics:

 int MyClass::showName(string id) const { ... } 

Here, the type const is a function (string) that returns int, and const is the function itself, i.e. body function.

0
source

Source: https://habr.com/ru/post/916433/


All Articles