Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about const member function [duplicate]

I met two explanation of const member function

class A{
  public:
  ...
  void f() const {}
  ...
}
  1. it means it could only access constant members;
  2. 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!

like image 789
skydoor Avatar asked Dec 27 '09 16:12

skydoor


People also ask

What is the const member function?

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.

What is const member function in Java?

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.

Is const function member of class?

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.

What is const function in C++ with example?

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 .


2 Answers

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:

  • Const-correctness (cprogramming.com)
  • Const correctness (C++ FAQ Lite)
like image 158
James Thompson Avatar answered Sep 20 '22 18:09

James Thompson


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.

like image 26
Ashish Avatar answered Sep 23 '22 18:09

Ashish