Member x
and y
are non-static data member of Enclosing
, which means that they only exist within a concrete object of Enclosing
class. Without a concrete object, neither x
nor y
exist. Meanwhile, you are trying to refer to x
and y
without an object. That can't be done, which is what the compiler is trying to tell you.
If you want to initialize members Inner::foo
and Inner::bar
from x
and y
, you have to pass a concrete object of Enclosing
type into the Inner
s constructor. For example
class Enclosing::Inner {
explicit Inner(const Enclosing& e) : foo(e.x), bar(e.y)
{}
//...
};
Extra note: in the original C++98 the inner class has no special privileges is accessing the outer class. With C++98 compiler you'd either have to give the inner class the necessary privileges (friendship) or expose the members x
and y
as public. However, this situation was classified as a defect in C++98, and it was decided that inner classes should have full access to outer class members (even private ones). So, whether you have to do anything extra with regard to access privileges depends on your compiler.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…