I'm browsing some code in the net, and I saw this two declaration,
const std::string strFoo = "MyFoo";
and
std::string const strBar = "MyBar";
I'm confused about the different placement of const keyword. What exactly its purpose?
Thanks!
I'm browsing some code in the net, and I saw this two declaration,
const std::string strFoo = "MyFoo";
and
std::string const strBar = "MyBar";
I'm confused about the different placement of const keyword. What exactly its purpose?
Thanks!
In this case it makes no difference.
For more complicated declarations it may make a big difference.
You could say that const
applies to what is to the left and if there's nothing there, it applies to what is to the right.
For example using const
qualifier on a pointer to int:
const int* ptr1; // (1.) pointer to const int
int const * ptr2; // (2.) same as 1.
int* const ptr3; // (3.) const pointer to int
const int* const ptr4; // (4.) const pointer to const int
int const * const ptr4; // (5.) same as 4.
For more info on how to read complicated type declarations se this: C Right-Left Rule