In C++11, I am trying to access a member variable of an enclosing class from a nested class in the following way:
struct Enclosing {
int a;
struct Nested {
int f() {
return a;
}
};
};
Even this doesn't compile using g++4.7.2 with -std=c++11, producing error messages of this form:
error: invalid use of non-static data member 'Enclosing::a'
As far as I understand, C++11 treats a nested class as a member of the class, so that supposedly a nested class can access every other member of the enclosing class. Did I do something wrong? Thanks in advance.
Update:
While my question seems to have an answer below, I am not convinced this shall be flagged as duplicate.
I am aware of discussions on the relationship between nested classes and enclosing classes before the C++11 standard, after a lot of searching before posting a question.
Previous relevant discussions like this cite some "updates" in C++11, e.g. C++ nested classes accessibility
But it was not very clear, at least from answers I've read, the full extent that C++11 is "different" from older versions on this matter.
Technically the solution to my question exists in older threads such as Nested class' access to enclosing class' private data members, a fact that had to be pointed out, however inane it makes me seem. But I did not come by any such answer that puts C++11 into context; at least, I don't think my question can be fairly deemed a "duplicate" of a question asked before the C++11 standard.
See Question&Answers more detail:os