Search code examples
c++constantsmember-functionsfunction-qualifier

Const operator in member function (Why can be in two different parts)?


We can find:

1)

const char *get() { return str; }

2)

int get() const { return A; }

What is the differences of "const" in this two different parts of the function?


Solution

  • The first returns a pointer to a char which is constant - the value of str cannot be modified. This is used to create an unchangable thing.

    The second returns an int (from A) which doesn't modify the state of the class - hence can be called when the class is constant.