Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am very familiar with C/C++ standard function declarations. I've recently seen something like this:

int myfunction(char parameter) const

The above is only a hypothetical example and I don't even know if it makes sense. I'm referring to the part AFTER the parameter. The const. What is this?

A more real example:

wxGridCellCoordsArray GetSelectedCells() const

This can be found here So what exactly is that text const doing at the end of the line?

like image 844
itsols Avatar asked Jul 24 '13 02:07

itsols


2 Answers

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.

like image 80
aaronman Avatar answered Sep 20 '22 15:09

aaronman


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
}
like image 21
Paul Renton Avatar answered Sep 23 '22 15:09

Paul Renton