I met two explanation of const member function
class A{
public:
...
void f() const {}
...
}
I think the second one is right. But why does the first one come out? Is there anything to be clarify?
Thanks!
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.
The Java equivalent of const In a language such as C++, the const keyword can be used to force a variable or pointer to be read-only or immutable. This prevents it from being updated unintentially and can have advantages when it comes to thread-safety.
Const member functions in C++ Like member functions and member function arguments, the objects of a class can also be declared as const. an object declared as const cannot be modified and hence, can invoke only const member functions as these functions ensure not to modify the object.
An object of a class may be declared to be const , just like any other C++ variable. For example: const Date birthday(7, 3, 1969); declares a constant object of the Date class named birthday .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With