Permanent Member Functions

After reading this , I understand that declaring a method as const prevents accidentally changing member variables of a class.

  • Commonly used const methods?
  • Should they be used for everything that member variables should not change?
+8
c ++ methods class const member
source share
8 answers

Yes, const should always be used when necessary.

It allows your compiler to test your application logic by statically claiming const-correctness for free

Some people even say that the value of const should be the default, and you should be forced to use mutable for mutable .

+14
source share

I use const to communicate developer intentions. If I assumed that the method is a pure request and not a modification function, I would both provide this and pass it using the "const" in the signature.

I urge you to take a look at Meyer's thoughts on problems that are free from side effects and the consequences that testing might have.

+6
source share

Only my testimony.

A few years ago, I was still against using const, only because of limitations in design and writing longer function signatures ... and so on ...

But one of my project managers always insisted, all the time, reminding me: "You must use the const function, this avoids accidents and makes no sense."

And one day I came across an impossible mistake. Days after days after days ... A nightmare. The design was too big for me, so I could understand it as a whole. I searched in vain until I decided that I was lost.

Then I spent two days redefining ALL the functions that should be const . I mean this, two days. (The recompilation was long, as it amounted to 5 million lines of project code).

And then: I just found the error ... rather, the compiler found the error for me: in the getter method, which was supposed to give me the preferred size of the gui control, the code actually calculated the size, but it also cached its size and updated its size .. Thus, changing the object.

Now, sometimes I forget to put const. But if I notice him, I will fix it.

+4
source share

Adding const to a member function allows it to call const references to the object, as it ensures that instance variables are not changed. Constant links are found in different places throughout the STL, so it makes sense to mark member functions as const, where the function does not intend to change the state of the object.

Note. You can mark some instance variables as mutable so that they can be changed even using const functions. This is useful for implementing search caches, for example.

+1
source share

Declaring a method that should not change member variables:

  • Provides what you think is happening, i.e. that you do not accidentally change the variable.
  • Announces to callers that this method does not modify member variables, eliminating the need to read code or rely on documentation that says so.

So, use const wherever it makes sense. They are not as widely used as we would like, although, most likely, because most developers do not see huge benefits.

+1
source share

If you forget to mark the accessor as const , the compiler does not allow calling the method on const objects or references to const objects. So yes, it's important to mark accessors as const .

+1
source share

If you have a const reference or a pointer (i.e. a pointer to const) of a class object, you can ONLY call member methods of the class.

So, if someone "forgot" to make the "get" method const, you would not be able to call it using the const link (there is a workaround with const_cast, but we don’t want to use it!).

So yes, if the class will not be modified by the method, then it must be const.

Note. There are some cases when you want to change a variable as an "implementation detail", for example, lazy loading or locking a mutex. In this case, you can still make the const method, but make this member variable "mutable".

If you are writing a virtual method, it must be const, if no derived class needs to be changed.

+1
source share

You should use the const keyword whenever possible.

This will prevent code errors.

This greatly improves code readability. Everyone who reads the title and sees the const keywords can immediately understand that the const method does not change the state of the object and can be used without fear that it will change the object, for example

+1
source share

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


All Articles