const object or object const?

Foo const *foo1;

The object that is pointing to is going to be constant, right?



Foo *const foo2;

The pointer 'foo2' is going to be constant and cannot be pointing to other stuffs, right?


const Foo *foo3;

This is same as first one, foo1, am I correct?
Yes, yes, and yes.
"const" makes the thing on its immediate left a constant, unless it is used at the beginning (in which case the leftmost item will be const).

T const*: T is const.
T* const: Pointer is const. T can still be edited.
T const* const: Both T and Pointer are const.
The "read backwards rule" is a useful way to remember this. Read the '*' as "pointer to"

1
2
3
4
T const* // <- a "pointer to a const T"
T* const // <- a "const pointer to a T"
T const* const  // <- a "const pointer to a const T"
// etc 
Topic archived. No new replies allowed.