问题
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.
回答1:
Here is the change within C++11 from cppreference;
Declarations in a nested class can use only type names, static members, and enumerators from the enclosing class (until C++11)
Declarations in a nested class can use any members of the enclosing class, following the usual usage rules for the non-static members. (since C++11)
int x,y; // globals
class enclose { // enclosing class
int x; // note: private members
static int s;
public:
struct inner { // nested class
void f(int i) {
x = i; // Error: can't write to non-static enclose::x without instance
int a = sizeof x; // Error until C++11,
// OK in C++11: operand of sizeof is unevaluated,
// this use of the non-static enclose::x is allowed.
s = i; // OK: can assign to the static enclose::s
::x = i; // OK: can assign to global x
y = i; // OK: can assign to global y
}
void g(enclose* p, int i) {
p->x = i; // OK: assign to enclose::x
}
};
};
In brief, within C++11, nested class can refer to types and static members of its enclosing class. In addition, it can refer to non-static members only when object of the enclosing class is given to the nested class. A nested class has access to members of its enclosing class including private members.
回答2:
To close this question I'll take this as an answer:
"No, it's not treated as a member of the class, it's just scoped inside of it like anything else. You'll need an instance of Enclosing to access it's members."
- this and several other comments addresses the problem in my code. Basically this is something that remains true for C++11.
来源:https://stackoverflow.com/questions/28003447/nested-class-member-access-on-c11