79

In C++, a function's signature depends partly on whether or not it's const. This means that a class can have two member functions with identical signatures except that one is const and the other is not. If you have a class like this, then the compiler will decide which function to call based on the object you call it on: if it's a const instance of the class, the const version of the function will be called; if the object isn't const, the other version will be called.

In what circumstances might you want to take advantage of this feature?

Tommy Herbert
  • 20,407
  • 14
  • 52
  • 57

5 Answers5

46

This really only makes sense when the member function returns a pointer or a reference to a data member of your class (or a member of a member, or a member of a member of a member, ... etc.). Generally returning non-const pointers or references to data members is frowned upon, but sometimes it is reasonable, or simply very convenient (e.g. [] operator). In such cases, you provide a const and a non-const versions of the getter. This way the decision on whether or not the object can be modified rests with the function using it, which has a choice of declaring it const or non-const.

Dima
  • 38,860
  • 14
  • 75
  • 115
27

It's there so you can make the compiler enforce whether you return a const object or a regular one, and still maintain the same method signature. There's an in-depth explanation at Const Correctness.

Boris Dalstein
  • 7,015
  • 4
  • 30
  • 59
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
9

Have a look at the behaviour of std::map::operator[]. The const version throws an error if you try to reference an invalid key, but the non-const version does an insert. The insertion behaviour is much handier than having to use std::map::insert (and will do an overwrite, moreover) but can't work for a const map.

RobH
  • 3,199
  • 1
  • 22
  • 27
5

You might want to use it to decide whether or not to return a const reference to an object or not. The STL's containers use a const overloaded begin() and end() function to decide whether to return a const_iterator or a normal iterator.

Jasper Bekkers
  • 6,711
  • 32
  • 46
0
#include <iostream>
using namespace std;
class base
{

public:
void fun() const
{
    cout<<"have fun";
}
void fun()
{
    cout<<"non const";
}

};
int main()
{
    base b1;
    b1.fun(); //does not give error
    return 0;
}

Here compiler won't give any error, because in case of const functions compiler converts this pointer to const this*. this third argument separates these two functions.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
user265906
  • 120
  • 1
  • 8
  • 2
    You have not explained why `const` qualifier is needed on member functions at all. – Tanveer Badar Nov 24 '19 at 10:08
  • @TanveerBadar it is needed, decision to pick is external. However i find this feature not to be consistent across multiple compilers for micro controllers, many give errors refusing to overload :\ – neu-rah Nov 23 '22 at 12:01