Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

c++ - Nested class member access on C++11

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...