1

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)

Community
  • 1
  • 1
hcc23
  • 1,180
  • 11
  • 17

2 Answers2

4

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.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Nested classes cannot access the outer class' private members.http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr061.htm – bash.d Feb 07 '13 at 19:09
  • I am not in front of a machine right now. Would be new to me!http://stackoverflow.com/questions/5013717/are-inner-classes-in-c-automatically-friends – bash.d Feb 07 '13 at 19:20
  • 3
    @bash.d: That has changed in the language. In C++03 nested classes are not considered *members* with respect to access specifiers, but in C++11 they are *members* just like member functions, and thus have the same access rights that any member function. – David Rodríguez - dribeas Feb 07 '13 at 19:22
  • 1
    @bash.d Whelp. I didn't know it had changed! – Joseph Mansfield Feb 07 '13 at 19:29
  • @sftrabbit so they got both of us ;) – bash.d Feb 07 '13 at 19:31