21
class Base {
 public:
  class FirstBase {
    friend class Base;
    int x = 10;
  };

  class SecondBase : public FirstBase {
   public:
    SecondBase() : FirstBase() {}
    void t() { std::cout << FirstBase::x; }
  };
};

This code compiles and works, but I don't understand why it works. Can explain or cite sources to read?

I use gcc version 11.4.0 std=C++17

Manarbek
  • 221
  • 5

2 Answers2

28

Base is a friend of FirstBase, and SecondBase can access anything Base can access.

From the latest draft:
class.access, item 2:

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

(Note that "nested" classes are members of the enclosing class.)

with the footnote

Access permissions are thus transitive and cumulative to nested and local classes.

Note that the inheritance is irrelevant - this is also fine:

class Base {
 public:
  class FirstBase {
    friend class Base;
    int x = 10;
  };

  class SecondBase {
   public:
    SecondBase() {}
    void t(FirstBase fb) { std::cout << fb.x; }
  };
};
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
16

Because Base is a friend of FirstBase. So inside Base you can access everything in FirstBase. And SecondBase is inside Base so SecondBase can access FirstBase. If you remove the friend relationship or you move SecondBase outside of Base, it'll break immediately as you expect.

class Base {
 public:
  class FirstBase {
    friend class Base; // This vill give access to FirstBase private members inside Base.
    int x = 10;
  };

  // SecondBase is inside Base so you can access FirstBase private members due to the friend relationship
  class SecondBase : public FirstBase {
   public:
    SecondBase() : FirstBase() {}
    void t() { std::cout << FirstBase::x; }
  };
};
simre
  • 647
  • 3
  • 9