This question already has an answer here:
I met two explanation of const member function
class A{
public:
...
void f() const {}
...
}
- it means it could only access constant members;
- it means it does not modify any members;
I think the second one is right. But why does the first one come out? Is there anything to be clarify?
Thanks!
You can examine all class member values in a const member function, and in some cases you can even change the value of member variables. The first explanation is incorrect, I don't know where it comes from. The second explanation is correct, but with a few exceptions.
There are some exceptions to this rule. You can also change mutable variables in a const member function, for example a member variable declared like this:
mutable float my_rank;
You can also break const-correctness in a class by const_cast'ing a reference to yourself like this:
Class* self = const_cast<Class*> (this);
While technically allowed in C++, this is usually considered poor form because it throws away all of the const modifiers of your design. Don't do this unless you actually have to, and if you find yourself having to do this quite a lot that suggests a problem with your design. The C++ FAQ covers this very well.
Here are two references in case you want to do more reading:
In a simple meaning , in const function you can't change the state of the object.
In const function this pointer behaves as const pointer to const data , where as in non-const function it behaves like 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 far as const data member is concern , you can access (read ) it from any member function whether its const or not.
As James Thompson has shown you can even change state of object by removing constness if you want like this.
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 Mutable data members can be changed in const function.
They are both correct.
A const member function can not alter the state of the object.
- This means it can read (but not modify) all member variables.
- This also means it can only call other const member functions
Other methods that guarantee not to change the state of the object.
Above James also mentions mutable members.
So I should also cover those here.
A mutable member variable is a variable that is not part of the state of the object (the compiler does not consider it part of the objects state). You should also treat it this way. Any member variable that holds state information about the object should NOT be marked mutable. You should only use it to hold temporary information that can be re-constructed from the objects state.
A simple example is a date-time object. Where the object has a method that converts the data/time into a readable string format. This string may be cached in the object in a mutable member for efficiency (so that you don't need to repeatedly build the string). But the string is not part of the object state (because it can be built from other members).
Also James mentions above casting away constness using const_cast.
Except under very special situations where you know the object CAN NEVER BE const doing this is considered universally a bad idea. As it leads directly to undefined behavior. If you find yourself needing to cast away constness then something very wrong in the design has happened in your program.
In fact I can only think of one situation where it happens normally. And then I am unwilling to commit it to code without first going and doing research to make sure I don't look silly.
I think the case 1 after some clarification may concern the situation when you have a const object of type A. In such a case you can only call its member functions declared as const like f() in this case. So according to your post you must assume that 'it' is the caller of the member functions on an object of type const A. Maybe you should review the definition you found having in mind this assumption.
来源:https://stackoverflow.com/questions/1966319/about-const-member-function