Can someone explain the use of const?


I come from a Java background, just to be clear const = “constant”. I have two questions, and I’d really appreciate any help just to make sure my thinking is right.

  1. Is using const is just a way to make sure we don’t alter the getter methods at a different stage, because the compiler will complain?

  2. And is this a standard in C++? Or is it just what Ben prefers.


  1. const at the end of member functions declares that the function doesn’t modify any of the class’ data. At the beginning of a variable declaration or function it declares that the value cannot be changed.

  2. It is best practice to use const wherever possible see here for a great example of why(if timestamp doesn’t work go to 26m48s):

The right hand side of the code is the assembly for the code on the left.


Thank you very much, excellent reply. So in basic terms, at the end of a
function it makes sure that it just returns or “gets” and doesn’t make
changes. I presume this is used now for more complicated programs in the

Thank you for taking the time to get the video as well!

Cheers :slight_smile:

It doesn’t change any of its own data. It’s free to change any data local to the function.

class Foo
    void test(int n) const;
    Int i = 5;

void Foo::test(int n) const
    int a = 4; 
    n = a; //ok
    a = 10; //ok
    i = n; //error
1 Like

Is my code and assumptions correct then?

I’m a bit confused now because “Int i” isn’t defined as constant. Its only the method test?

Apologies for my confusion.

Best wishes,

I wrote that on my phone. It should be int not Int, stupid auto capitalisation.

And that’s what const at the end of a member function does, it prevents the class’ data members from being changed by that function. Similarly if Foo had more data members like float f; and bool b. i, f and b wouldn’t be able to be modified by Foo::test since that function is const.

1 Like

Awh ok I get it now, just clicked.

I appreciate the help, thank you very much!

Best wishes,

Just tagging @ben since it’s pretty cool to see what a simple const can do, though he does go onto say that he only got that result with clang with gcc’s standard library.

Thank you Dan, excellent resources.

Hello Reece, I just wanted to point out that the line std::cout << i = "5"

  • wont compare i to 5 because i is int and 5 is a string, and to compare you will need a double equal sign ==
  • wont print i because it’s missing a semicolon, should be something like std::cout << i ;
  • wont assign 5 to i because being of different types

in order to create an integer const, we should use constexpr int i = 5;

:slight_smile: I am learning a lot, thanks @ben

In C++ Getters should only GET values from classes and return them within our code, therefore in many cases they should be const. In C++ we use SET functions, which are used to change variables and they always receive variable e.g. SetMaxTries (int MaxTries). Set functions often don’t return any value because they are only used for changing private data within class.
On the other hand you can use const when you initiate variables e.g. const int MyMaxTries = 5. This wouldn’t allow you to change the value of MyMaxTries.
I hope that helps.

You’re welcome, thanks for thanking me :slight_smile:

Privacy & Terms