Access of nested classes (which behave like friend

2019-08-13 13:38发布

问题:

Without long delay, here the code which I have no clue why it does what it does:

#include <iostream>

class A {
private:
  void print() { std::cout << "A.print() called" << std::endl; };
public:
  template<typename Foo>
  class B; //NOTE: no friend!
public:
  A();
  B<double>*  bd;
  B<int>*     bi;
}; 

template<typename Foo>
class A::B{
  A* callback;
public:
  B(A* a):callback(a){};
  void print() { callback->print(); }; // Why is this working ???
};

A::A():bd(new B<double>(this)),bi(new B<int>(this)){}

int main(int argc, char **argv)
{
  A a;
//   a.print(); // error: ‘void A::print()’ is private
  a.bd->print();
  a.bi->print();

  A::B<char> c(&a);
  c.print();

  A::B<double> d = *a.bd;
  d.print();

  return 0;
}

Well, it creates this ouput:

A.print() called
A.print() called
A.print() called
A.print() called

But why?

Background

I initially started my journey down the rabbit hole when I encountered a problem which I through to have to do with friends. So I read friend declaration not forward declaring (and the mentioned answers here and here). So while trying to set up an easy example (the result of which you see above), I found that I actually don't seem to need friend at all.

Question

So here is the bottom line question: Why does an instance of A::B have access to A's private function A::print()? (although I do realize that I might misunderstand what my children are--children as opposed to base vs. derived)

回答1:

because nested class is a member of the enclosing class

standard $11.7.1

"A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed"

and the usual access rules specify that:

"A member of a class can also access all the names to which the class has access..."

specific examples has been given in the standard:

class E {
    int x;
    class B { };

    class I {
        B b; // OK: E::I can access E::B
        int y;
        void f(E* p, int i) {
            p->x = i; // OK: E::I can access E::x
        }
    };
}


回答2:

A nested class (or inner class) has access to the privates of the class it is nested within. It is in some sense already friends with that class. It is similar to the way that any A object has access to the privates of any other A object.

You use friend for classes that are defined outside of your class that you want to access its privates. Here's a simple example of this:

struct B;

class A
{
  int x;
  friend struct B;
};

struct B
{
  void someFunc() {
    A a;
    a.x = 5;
  }
};

int main(int argc, const char* argv[])
{
  B b;
  b.someFunc();
  return 0;
}

Without making B a friend of A, it would not be able to access A's member x.