For example, is
int const x = 3;
valid code?
If so, does it mean the same as
const int x = 3;
?
For example, is
int const x = 3;
valid code?
If so, does it mean the same as
const int x = 3;
?
They are both valid code and they are both equivalent. For a pointer type though they are both valid code but not equivalent.
Declares 2 ints which are constant:
int const x1 = 3;
const int x2 = 3;
Declares a pointer whose data cannot be changed through the pointer:
const int *p = &someInt;
Declares a pointer who cannot be changed to point to something else:
int * const p = &someInt;
Yes, they are the same. The rule in C++ is essentially that const
applies to the type to its left. However, there's an exception that if you put it on the extreme left of the declaration, it applies to the first part of the type.
For example in int const *
you have a pointer to a constant integer. In int * const
you have a constant pointer to an integer. You can extrapolate this to pointer to pointers, and the English may get confusing but the principle is the same.
For another dicussion on the merits of doing one over the other, see my question on the subject. If you are curious why most folks use the exception, this FAQ entry of Stroustrup's may be helpful.
Yes, that is exactly the same. However, there is difference in pointers. I mean:
int a;
// these two are the same: pointed value mustn't be changed
// i.e. pointer to const value
const int * p1 = &a;
int const * p2 = &a;
// something else -- pointed value may be modified, but pointer cannot point
// anywhere else i.e. const pointer to value
int * const p3 = &a;
// ...and combination of the two above
// i.e. const pointer to const value
const int * const p4 = &a;
From "Effective C++" Item 21
char *p = "data"; //non-const pointer, non-const data
const char *p = "data"; //non-const pointer, const data
char * const p = "data"; //const pointer, non-const data
const char * const p = "data"; //const pointer, const data
const pointer: pointed value may be modified, but pointer cannot point
anywhere else
It is the same in meaning and validity.
As far as I know, const only get complex whenever it involves pointer.
int const * x;
int * const x;
are different.
int const * x;
const int * x;
are same.
I found that if always write const in right, it will becoming easier to read a complex pointer type, for fun.
For example:
// Just append const or pointer to
int x; // data
int const cx; // const data
int *px; // pointer to data
int const *pcx; //pointer to const data
int * const cpx; // const pointer to data
int const * const * * const cppcpcx; // const pointer to (pointer to (const pointer to const data))
I have an idea how to interpret (non)const pointers and data. Split it with *.
Data type | Star sign | Pointer type | Combine | Data | Pointer |
---|---|---|---|---|---|
int | * | p | int *p | non-const | non-const |
const int | * | p | const int *p | const | non-const |
int | * | const p | int *const p | non-const | const |
const int | * | const p | const int *const p | const | const |
// 1.
int x1 = 1;
int x2 = 2;
const int* p = &x1;
//*p = 3; // variable data is CAN NOT BE changed through pointer
p = &x2; // pointer CAN POINT to other variable
cout << *p << endl; // OUTPUT: 2
// 2.
int y1 = 1;
int y2 = 2;
int* const t = &y1;
*t = 3; // variable data is CAN BE changed through pointer
//t = &y2; // pointer CAN NOT POINT to other variable
cout << *t<<endl; // OUTPUT: 3