About const member function

I met two explanations of the const member function

class A{ public: ... void f() const {} ... } 
  • means that he can only apply to permanent members;
  • this means that he does not change any members;

I think the second one is correct. But why did the first come out? Is there anything that can be clarified?

Thanks!

+7
c ++ const
source share
4 answers

You can check all the values ​​of class members in a const member function, and in some cases you can even change the value of member variables. The first explanation is incorrect, I do not know where it came from. The second explanation is true, but with a few exceptions.

There are some exceptions to this rule. You can also modify mutable variables in a member function, for example, a member variable declared as follows:

 mutable float my_rank; 

You can also break const-correctness in a class by pointing const_cast to a link to itself as follows:

 Class* self = const_cast<Class*> (this); 

Although technically permitted in C ++, this is usually considered a bad form, as it discards all the constant modifiers of your design. Do not do this if you really do not need, and if you need to do it enough, which suggests a problem with your design. The C ++ FAQ about this is very good.

Here are two links if you want to read more:

+18
source share

In a simple meaning, in the const function you cannot change the state of an object.

In the const function, this pointer behaves like a const pointer to const data strong>, where, as in the non-constant function , it behaves like a const pointer to data .

 void foo() const --> const ClassName * const this (so you can't alter data) void foo() --> ClassName * const this (so you can alter data) 

As for the const data element, you can access (read) it from any member function, regardless of whether it is a constant or not.

As James Thompson showed, you can even change the state of an object by deleting a constant if you want.

 class Bar { int bar; public: void foo() const { this->bar = 0; //flashes error Bar * const thisClass = const_cast<Bar * const>(this); thisClass->bar = 0; } }; 

Also, members of Mutable data can be changed in const functions.

+5
source share

They are both correct.

The const member function cannot change the state of an object .

  • This means that it can read (but not change) all member variables.
  • It also means that it can only call other constant member functions.
    Other methods that guarantee not to change the state of an object.

Variable members are also mentioned over James.
Therefore, I must also highlight them here.

A modified member variable is a variable that is not part of the state of the object (the compiler does not consider it part of the state of objects). You should also relate to it this way. Any member variable that contains information about the state of an object should NOT be marked as mutable. You should only use it to store temporary information that can be rebuilt from the state of objects.

A simple example is an object with a date. Where the object has a method that converts data / time into a readable string format. This line can be cached in an object in a mutable member for efficiency (so you don't have to create the line multiple times). But the string is not part of the state of the object (because it can be built from other members).

In addition, James mentions above, crowding out the constellation using const_cast .

Except in special situations where you know an object CANNOT be const, this is considered a universal bad idea. Because this leads directly to undefined behavior. If you need to drop the constant, then something very wrong will happen in your program in your program.

In fact, I can only think of one situation when this happens normally. And then I do not want to comment on it before I go and do research to make sure that I do not look stupid.

+2
source share

I think that case 1, after some clarification, may concern the situation when you have a const object of type A. In this case, you can only call its member functions declared as const, like f () in this case. So, according to your message, you should assume that β€œit” is the caller of the member functions of an object of type const A. Perhaps you should consider the definition that you found with this assumption in mind.

+1
source share

All Articles