I have some code:
const string MY_STRINGS[3] = { "A", "B", "C" };
and when I try to do this:
int main() {
MY_STRINGS[1] = MY_STRINGS[2];
}
I get the following error:
Passing ‘const std::string’ as ‘this’ argument of ‘std::basic_string… & std::basic_string::operator= …’ discards qualifiers.
Sure. Makes sense. I can’t assign to a const string.
But when I do the following:
int main() {
const string SOME_STRINGS[3] = MY_STRINGS;
MY_STRINGS[1] = MY_STRINGS[2];
}
this compiles and works. What has changed, and why does it now compile?
I believe GCC copies the array. Thus ensuring the const array is immutable and the variable array mutable.
I tested this on GCC version 4.1.2