简体   繁体   English

如何从其他类私有内部类访问数据?

[英]How to access data from other classes private inner class?

I get it how to access data from inner class, when we are talking about that same class. 当我们讨论同一个类时,我知道如何从内部类访问数据。 But lets say we have a class called "ben", and other one is "bob". 但是我们假设我们有一个名为“ben”的类,而另一个是“bob”。 Now "bob" has inner private class called "john" and in john, there is his phone number variable. 现在“bob”有内部私人类叫做“john”,而在john中,有他的电话号码变量。 I would like to access johns phone number form class ben. 我想访问johns电话号码表班本。

I hope I explained it at least "okay". 我希望我解释它至少“好”。 So, is it possible to do this, or should I just make a getter method for bob class? 那么,是否可以这样做,或者我应该为bob类制作一个getter方法?

If you want to access john from ben, it means that john should not be private to bob. 如果你想从ben访问john,这意味着john不应该是私有的bob。 The whole point of making john private to ben is to hide it from others. 将约翰私有化为ben的重点是将其隐藏起来。

So to solve your problem, make john a normal class since it is needed by more than one class. 因此,要解决您的问题,请将john设为普通类,因为多个类需要它。

john has to declare that ben is his friend or ben can't have his phone number 约翰必须声明本是他的朋友或本不能拥有他的电话号码

class ben {
   string john() { return bob::john::phone; }
};

class bob {
  class john
  {
     friend class ben;
     static string phone;
  };
};

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

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