Which of these are more correct and more widely used? The actualy question is the last one, I think the behavior changes.
int *test; //for this, it is probably both?
int* test;
int& test;
int &test;
Actual question:
const int test;
int const test;
const int* test;
int* const test; //<-- I guess this also have a different meaning if I consider the last one?
const int& test;
int const& test; //also, considering last one, any difference?
const int*& test;
int* const& test; //<-- this is the correct one for stating "I won't modify the passed object, but I may modify the one pointed"? I have had problems with the other one sometimes, does the other one has some other meaning?
const int* const& test; //<-- another meaning?
Also I would be glad if you could point out if you know any visual "ambiguations" in this subject.
Where you place the space around &
or *
(or indeed if you have one or more spaces one or both sides) makes absolutely no difference. The placement of const
does make a difference;
const int *test;
means that what test
points to isn't being changed. So:
int b = 42;
*test = 42;
test = &b;
The *test = 42;
would be illegal, but assigning test to a new address is valid.
int * const test;
means that test
doesn't can't change it's value, but what it points to can:
int b = 42;
*test = 42;
test = &b;
now test = &b;
is invalid.
const int& test;
int const& test; //also, considering last one, any difference?
Both the same. The const and int are the same side of &
.
This one:
const int*& test;
means we have a reference to a int *
where the value can't be changed. Perfectly valid, we can use the following:
test = &b;
these two:
int* const& test
const int* const& test;
is a reference to an int *
and const int *
respectively, and we can't change the pointer itself - so no point in passing it by reference.