Possible Duplicate:
What is the difference between const int*, const int * const, and int const *?
I know two variations of pointer variables in C++.
Say I have
mystruct{
int num;
}
Variation 1:
const mystruct* m1; means the member variables in m1 can not be altered, for instance, m1->num = 2 would produce an error.
Variation 2:
mystruct *const m2 = m1; means once m2 is set to point to m1, an error would be produced if you subsequently set m2 =m3.
However, there seems to be a third variation, that I am not certain the property of:
Variation 3:
mystruct const * m3;
What does this mean?
Variant 3 is exactly the same as variant 1. The
constapplies to whatever is to the left of it. If there is nothing (first variant), it applies to the thing that is right of it. Also, you’re missing one variation.I personally prefer the second version to the first, as that is what the compiler parses and when you read the type from right to left, it is clearer. 🙂 Also, that’s what I did in the comments – intuitive, isn’t it?
Oh, yeah, all of the above also applies to
volatile. Together they’re counted ascv-qualifiers(cv=const volatile).And as a last point, for future references: There’s always cdecl.org. As long as you don’t mix in C++ types, you can find out the meaning of potentially any declaration. Edit: Interestingly enough, it chokes on
int const i;. I think I’ll need to research if the variable ordering of cv-qualifiers was introduced in C++.