Reputation: 325
I don't understand something here. In the following code I have defined an integer and a constant integer.
I can have a constant pointer (int* const) point to an integer. See the fourth line of code.
The same constant pointer (int* const) can not point to a constant integer. See the fifth line.
A constant pointer to a const (const int* const) can point to a constant integer. That's what I would expect.
However, the same (const int* const) pointer is allowed to point to a non constant integer. See the last line. Why or how is this possible?
int const constVar = 42;
int variable = 11;
int* const constPointer1 = &variable;
int* const constPointer2 = &constVar; // not allowed
const int* const constPointer3 = &constVar; // perfectly ok
const int* const constPointer4 = &variable; // also ok, but why?
Upvotes: 2
Views: 3480
Reputation: 12797
int const constVar = 42; // this defines a top-level constant
int variable = 11;
int *const constPointer1 = &variable;
int *const constPointer2 = &constVar; // <-- not allowed, because you
// can change constant using it
// perfectly ok. here you can't change constVar by any mean.
// it is a low-level constant.
const int *const constPointer3 = &constVar;
// also ok, because it says you can't change the value using this pointer.
// but you can change value like variable=15.
const int *const constPointer4 = &variable;
// you get error assignment of read-only location,
// because that pointer is constant and pointing to read-only memory location.
*constPointer4 = 5;
Upvotes: 3
Reputation: 1
Line #4:
int* const constPointer2 = &constVar;
Here it should not be allowed because the int* const
part of int* const constPointer2
means that the pointer is constant, and then you go ahead and assign it to &constVar
.
Upvotes: 0
Reputation: 70526
You can always decide not to modify a non-const variable.
const int* const constPointer4 = &variable;
Just parse the definition: constPointer4
is a const (i.e you can't change what it is pointing to anymore) pointer to a const int (i.e. variable
). This means that you can't modify variable
through constPointer4
, even though you can modify variable
by other means.
THe other way around (accessing a const variable through a non-const pointer), you would need a const_cast
.
Why is a pointer to const useful? It allows you to have const
member functions in classes where you can guarantee to users that that member function does not modify the object.
Upvotes: 1
Reputation: 76305
A pointer to a const object cannot be used to modify that object. It doesn't matter whether someone else can modify it; it just can't be done through that pointer.
int i; // modifiable
const int *cip = &i; // promises not to modify i
int *ip = &i; // can be used to modify i
*cip = 3; // Error
*ip = 3; // OK
Upvotes: 0
Reputation: 5552
const has less access rights than non const, thats why it is allowed. You will not be able to change "variable" through the pointer but that is not breaking any rules.
variable = 4; //ok
*constPointer4 = 4; //not ok because its const
You use this "const pointer to non const variable" situation a lot when calling functions.
void f(const int * const i)
{
i=4; //not ok
}
f(&variable);
Upvotes: 1