Home more const
Post
Cancel

more const

As I was trying to understand myself due to multiple ways a const might exist in code, I have found one neat little tip I found regarding const that helps us identify what that const means. I found that out through this website The C++ ‘const’ Declaration: Why & How. The key setence I want to highlight is:

1
Basically ‘const’ applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to whatever is its immediate right).

So for example, very simply:

1
const int i;

Nothing on the left, so i is a constant integer. The most basic const of all.

1
const int * i;

Nothing on the left, so i is a variable pointer to a constant integer.

1
int const * i;

i is the same as above since const refers to the “thing” to its left.

1
int * const i;

The left of const is *, so this means this is a constant pointer to an integer variable.

1
int const * const i;

There are two const here each with something to its left. i is a constant pointer to a constant integer.

Those are some basic tips and there will be more const stuff as always!

This post is licensed under CC BY 4.0 by the author.