0
class A
{
   public:
   class B
   {
       private:
         int ii;

   };
   void print(){cout<<B::ii<<endl;}
};

My compiler says I can't access the integer inside ii; I search through this site and come up with friend key I change it into

class A
{
   public:
   class B
   {
       private:
         int ii;

   };
   friend void print(){cout<<B::ii<<endl;}
};

But it doesn't work too. Can you help me ? I am stuck.really really really sad as no way out!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Dalton
  • 181
  • 1
  • 8

3 Answers3

4

A contains B. But that doesn't mean that A is allowed to access the privates of B. It can only do that if B makes A a friend.

class A
{
   public:
   class B
   {
       private:
         int ii;
         friend class A;
   };
   void print(){cout<<B::ii<<endl;}
};

Also, ii is a non-static member of B. You cannot access it with B:: syntax; you need an instance of the type B.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Can you write me your code ? You words are just high and big, I can't understand well – Dalton Aug 09 '11 at 03:45
  • ,,,Thank you very much Do you know why friend is prefered use in nested classes ? – Dalton Aug 09 '11 at 03:51
  • It's not. It's just a way to override encapsulation of a class for specific instances. Quite often, it means you haven't thought things through very well with your design, though there are very specific exceptions. – Michael Dorgan Aug 09 '11 at 04:07
1

Class A has no special access privelages to the members of class B and has to respect the private property of ii. You can access ii by creating set/get functions for class B or by making A a friend of B. You also need an actual object of type B.

class A
{
   public:
   class B
   {
       private:
         int ii;
         friend class A;
   };

   B m_B;
   void print(){cout<<m_B.ii<<endl;}
};
Pepe
  • 6,360
  • 5
  • 27
  • 29
  • Thanks, you rreply is the no difference to Nicol Bolas . Thanks anyway – Dalton Aug 09 '11 at 03:54
  • @ILuvMoney It is different. It has a member variable named m_B of type B. – Pepe Aug 09 '11 at 03:55
  • oh sorry I don't see it. Do you know why friend is prefered use in nested class. I read a post about firend of site, it tells me so. Is it cheated me ? I am confusse – Dalton Aug 09 '11 at 03:58
  • 1
    @ILuvMoney, I think you have quite a few misunderstandings about object orientation, friend and private isn't your only one. At this stage I would forget about using private and forget about using nested classes, it's only going to make your life more difficult. Make everything public. You are still going to get errors because of your other misunderstandings but at least you'll only have to figure out one thing at a time. Later you can come back to the issues of private and friends and nested classes when you have your other issues sorted out. – john Aug 09 '11 at 05:42
0

The Standard text is very clear about this:

The C++ Standard (2003) says in $11.8/1 [class.access.nest],

The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be obeyed. The members of an enclosing class have no special access to members of a nested class; the usual access rules (clause 11) shall be obeyed.

Now, regarding the second case in your question, the print() function is a friend of A, that means, it can access private members of A only, not that of B, since print() function is NOT a friend of B. And making print() friend of A does NOT automatically make it friend of B also.

By the way, the Standard quotation has one defect. It says the nested classes don't have access to private members of the enclosing class. But in C++0x, it has been corrected: in C++0x, nested classes do have access to private members of the enclosing class (though the enclosing class still doesn't have access to private members of the nested classes).

See this Defect Report :


Somewhat related topic:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • thank you so much, but I have wonder why friend is prefered use to access private member of nested class in my example? Or I read wrong ? – Dalton Aug 09 '11 at 04:08
  • @ILuvMoney: See my answer. I've added more explanation to it. – Nawaz Aug 09 '11 at 04:27