i have a class name X, what is the difference between "const X a" and "X const a"
views:
157answers:
4Nothing.
A const
qualifier applies to whatever is immediately to its left. If there is nothing to its left then it applies to whatever is immediately to its right.
No difference at all.
See: http://www.parashift.com/c++-faq-lite/const-correctness.html
In this case, there's no difference at all.
When you have a pointer or a reference, a change that might look almost the same is significant though. Given something like:
T * a;
The position of const
(or volatile
) relative to the asterisk is significant:
T const * a;
T * const a;
The first one says that a
is a pointer to a const T
(i.e., you can't modify the T object that a
refers to). The second one says that a
is a const point to a (non-const) T -- i.e., you can modify what a
points at, but you can't modify the pointer itself, so you can't point it at a different object. Of course, you can also do both:
T const * const a;
This means you can't change the pointer itself or the T
object it refers to.