c++ function syntax/prototype - data type after brackets

The const says that the function will not change any of the data members of this unless they are marked mutable.
Only a member function can be marked const so this means that none of the members will be changed inside the function.


The const keyword, when shown after a function, guarantees the function caller that no member data variables will be altered. It also changes the function signature, which is something less known to some C++ programmers. You can actually overload a function in C++ by adding the const keyword after a function with the same name. For example:

void changeValue() const;
void changeValue();

Both of the above functions are valid and an overload of each other. I often see some C++ API's and frameworks use this overload to avoid a plethora of compile errors when users call these functions within const and non-const functions. Whether this is good C++ software engineering is up for debate. I would imagine it is bad practice, but it is good to be aware that it changes the function signature.

For instance given this class,

// In header
class Node {

public:

Node();

void changeValue() const;

~Node();

private:

int value;

};

// in .cpp

void Node::changeValue() const {
    this->value = 3; // This will error out because it is modifying member variables
}

There is an exception to this rule. If you declare that a member data variable is mutable, then it can be altered regardless if the function is declared to be const. Using mutable is for the rare situation where an object is declared constant, but in practice has member data variables which need the option to change. One potential example of its use is caching a value that you may not want to repeat the original calculation. This is typically rare... But it is good to be aware of it. A good reference of software engineering decisions around Mutable is the concept of bitwise const vs conceptual const. With bitwise const, the programmer is informing the reader that when const is present, no bits for that const object shall change without a const_cast. With conceptual const, the idea is that the user of the class should not care whether the bits of the mutable variable should change as it does not impact the usage of the class from the user's perception. Here is a good article explaining the difference and the ups and downs of using Mutable - https://www.cprogramming.com/tutorial/const_correctness.html

For instance given this class,

// In header
class Node {

public:

Node();

void changeValue() const;

~Node();

private:

mutable int value;

};

// in .cpp

void Node::changeValue() const {
    this->value = 3; // This will not error out because value is mutable
}

It's a "defensive programming" technique to help guard against your own programming errors. With const against a function parameter, you are stating that the function should not modify that parameter, and adding the const causes the compiler to prevent you from inadvertently doing so. Similarly, if you write a member function which shouldn't change any member variables of your class, then you can declare the whole function const like that, and it will prevent you from doing so.

It also helps to make your code self-documenting. Adding const to a parameter tells a user that 'this function does not modify this parameter'. Adding const to a member function tells the user that 'this function does not modify any members of the class' (except explicitly mutable ones).

Restricting access to something except for those occasions where you really need it should generally be considered to be a good thing. It's the exact same reason why you don't routinely log onto your own system as root, even though you could, and you'd have more power if you did.