0

I've seen the following:

void function_name(std::string& txt) const;

What is the purpose of the const in the above prototype? The return type of the function is void so there is no return type, it looks completely pointless to me, I've seen this a lot, also when passing in parameters like:

void function_name(const int a);

Why would const add anything to the above, it cannot be changed in the function anyway if its not a pointer or reference?

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • 1
    For the second one, to prevent someone maintain the code and accidentally write a line `a=/*a magical number*/` and break the function. – Louis Go Aug 03 '20 at 05:17
  • please ask 1 question per post. Your two questions have two completely different answers. – bolov Aug 03 '20 at 05:31

3 Answers3

3
void function_name(std::string& txt) const;

const mostly means this is a const member function and it's not supposed to change the state of the object. So if the function changes any of the member variables it will lead to compile error.

For the second case:

void function_name(const int a);

This const means that the function is not suppose to change variable a in its function body. Note, however, that const would not be required for basic type like int or bool. Also it's most likely applied when pass-by-reference (not pass by value like this).

So a typical usage would more like this:

void function_name(const some_non_trival_type& a);
artm
  • 17,291
  • 6
  • 38
  • 54
1

That's said it's okay in the beginning without const.

void function_name(const int a)
{
    int b = 10;
    // complex things ...
    std::cout<< a+b;
}

One day someone thought a new line would magically increase the performance of the function

void function_name(const int a)
{
    a = 10; // Beep! It doesn't compile.
    int b = 10;
    // complex things ...
    std::cout<< a+b;
}

Then this const save the world before any unit tests have been executed.

I'd say this const might have a semantic meaning to tell others "don't modify it because this function works properly according to parameter's constness".

Louis Go
  • 2,213
  • 2
  • 16
  • 29
1

The const method prototype enables you to use such method also for const objects like:

struct X
{
    void function_name(std::string& txt) const {}
};

int main()
{
    const X x;
    std::string s{"Hallo"};
    x.function_name(s); // will not work if the function is not marked as const!
}

The second case

void function_name(const int a);

gives the compiler a hint that you will ( and can ) never change the content of the const variable. That allows the compiler to optimize it. If you use a non trivial object like:

 void function_name(const XYZ x);

it makes more sense, because if it is not const, the compiler must create a full copy of the object for that function. That may also be optimized out, but in case of const there is simply no need to take a copy as nothing will be altered. By standard clever compilers the internal generated code can be equivalent to:

 void function_name(const XYZ& x);

or

 void function_name(XYZ& x);

For the given example of using an int, it is has more or less no effect, but clarifies the interface.

Klaus
  • 24,205
  • 7
  • 58
  • 113