C++ 如何访问嵌套类的私有成员?

C++ 如何访问嵌套类的私有成员?,c++,C++,A.hpp class A { public: class B { int x; public: B(int f); }; void alpha(B *random); }; void A::alpha(A::B *random) { // access x here, how to do it? } class A { public: class B { int x; public: B(int f); }

A.hpp

class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  void alpha(B *random);
};
void A::alpha(A::B *random)
{  
  // access x here, how to do it?
}
class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  virtual void alpha(B *random) = 0;
};
class C : public A
{
  public:
  virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{  
  // access x here, how to do it? 
}
A.cpp

class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  void alpha(B *random);
};
void A::alpha(A::B *random)
{  
  // access x here, how to do it?
}
class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  virtual void alpha(B *random) = 0;
};
class C : public A
{
  public:
  virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{  
  // access x here, how to do it? 
}
私有变量正在某个地方设置,我想在这个
alpha
函数中访问该值。如何访问
alpha()
内部的
x

编辑:第二个问题: A.hpp

class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  void alpha(B *random);
};
void A::alpha(A::B *random)
{  
  // access x here, how to do it?
}
class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  virtual void alpha(B *random) = 0;
};
class C : public A
{
  public:
  virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{  
  // access x here, how to do it? 
}
C.hpp

class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  void alpha(B *random);
};
void A::alpha(A::B *random)
{  
  // access x here, how to do it?
}
class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  virtual void alpha(B *random) = 0;
};
class C : public A
{
  public:
  virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{  
  // access x here, how to do it? 
}
C.cpp

class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  void alpha(B *random);
};
void A::alpha(A::B *random)
{  
  // access x here, how to do it?
}
class A
{
  public:
  class B
  {
    int x;
  public:
    B(int f);
  };
  virtual void alpha(B *random) = 0;
};
class C : public A
{
  public:
  virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{  
  // access x here, how to do it? 
}

您可以使类
A
成为类
B
的“朋友”,从而允许
A
访问
B
的私人成员

合并文件以便于编译:

A类
{
公众:
B类
{
A类朋友;//***这里***
int x;
公众:
B(int f);;
};
空隙α(B*随机);
};
void A::alpha(B*随机)
{
int x=随机->x;
}
int main(){}

如果一个类是另一个类的
朋友,它可以访问另一个类的
私有成员,如本例所示:

class A {
 public:
  class B {
    friend class A;
    int x;

   public:
    B(int f);
  };
  void alpha(B *random) { random->x = 10; }
};

如果
B
a
的私有嵌套类,则可以将其成员公开。他们可以访问
A
而不是其他任何东西。对不起,这是个错误。B类是公共的,但其中的“int x”是私有的@NathanPierson感谢您的回复。我有一个类似的第二个问题。我在问题中添加了它作为编辑。那么我需要在C.hpp或A.hpp中添加朋友吗?@galaxy2096-您需要编辑的头文件是包含
B
定义的头文件(在您的例子中是A.hpp)。具体来说,类
B
需要将需要访问
B
s私有成员的所有其他类声明为
朋友。因此,如果
A
的成员需要访问权限(例如
A::alpha()
A
的其他成员),则
A
需要成为
朋友
C
的成员需要访问权限,则
C
需要成为朋友。类的友谊不是继承的-将
A
声明为
friend
不会导致
C
成为
friend
,相反,将
C
声明为
friend
不会导致
A
成为
friend
更改
C::alpha()
的声明,从
void alpha(B*)
无效alpha(A::B*)
。或者,使用C++11及更高版本,在
C
的定义中使用
override
(即,执行
void alpha(B*)override
将导致编译器告诉您
C::alpha()
不是覆盖
A::alpha()
,而是覆盖
void alpha(A::B*)重写
会起作用。@Peter是的,我以前就有过。我忘了在问题中添加它。但是当我试图在“friend class A”下面添加“friend class C”时,它抛出一个错误,说它不知道class C,然后将
class C
的声明放在任何类之外)在定义
B
之前,或将好友声明更改为
friend class::C
(或两者兼有)。您需要记住,您要声明/定义的每个类都在一个范围内,并且任何声明(例如作为好友)或用法都需要正确(明确地)定义合格。如果编译器认为它位于与您不同的作用域中,则编译器获胜。