简体   繁体   中英

Nested class' access to enclosing class' private data members

I'm having trouble implementing a nested class who's constructor is initialized with some of the enclosing class' private data members.

Example:

Header File:
class Enclosing {
   //...Public members
   //...Private members
   int x, int y
   class Inner; // Declaration for nested class
};

Impl. File:
// Stuff...
class Enclosing::Inner {
    explicit Inner() : foo(x), bar(y) // foo and bar are data members of Inner
    //...
};

I get an invalid use of non-static data member error. Is there something I'm missing when it comes to nested class access to its enclosing class' members?

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.

The problem with your code is not visibility , as pointed out by AndreyT , but that an instance of the Inner class is not bound to a concrete instance of the Enclosing class. The In other words when constructing an Inner the compiler has no way of know which object to take the x and y values from.

You will have to explicitly provide an instance of the Enclosing class to the constructor of the Inner class as so:

class Enclosing
{
private:
  int x;
  int y;

  class Inner
  {
  private:
    int foo;
    int bar;

  public:
    explicit Inner(const Enclosing& e)
      : foo(e.x), bar(e.y) 
    { }
  };
};

嵌套类无法访问封装类的privet数据成员。如果我们尝试访问封闭类的privet成员,则显示错误,它只能访问封闭类的公共数据成员.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM