简体   繁体   English

嵌套类'访问封闭类'私有数据成员

[英]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. invalid use of non-static data member错误。 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. 成员xyEnclosing非静态数据成员,这意味着它们只存在于Enclosing类的具体对象中。 Without a concrete object, neither x nor y exist. 没有具体的对象, xy都不存在。 Meanwhile, you are trying to refer to x and y without an object. 同时,您试图在没有对象的情况下引用xy 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. 如果要从xy初始化成员Inner::fooInner::bar ,则必须将Enclosing类型的具体对象传递给Inner的构造函数。 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. 额外注意:在原始的C ++ 98中,内部类没有特殊权限访问外部类。 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. 使用C ++ 98编译器,您必须为内部类提供必要的权限(友谊)或将成员xy公开为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). 但是,这种情况在C ++ 98中被归类为缺陷 ,并且决定内部类应该具有对外部类成员(甚至是私有成员)的完全访问权限。 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. 与您的代码的问题是不可见性 ,正如指出的AndreyT ,但该实例Inner类是未绑定到的具体实例Enclosing类。 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. 换句话说,在构造Inner ,编译器无法知道从哪个对象获取xy值。

You will have to explicitly provide an instance of the Enclosing class to the constructor of the Inner class as so: 您必须向Inner类的构造函数显式提供Enclosing类的实例,如下所示:

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成员,则显示错误,它只能访问封闭类的公共数据成员.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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