Why does the derived class have access to the private field of the base class?

Why does the derived class have access to the private field of the base class?


10

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

Share
Improve this question

New contributor

Manarbek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

2 Answers
2

Reset to default


14

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; }
  };
};

Share
Improve this answer

1

  • friendship is not transitive, aside from that, true enough.

    – Deduplicator

    1 hour ago


9

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; }
  };
};

Share
Improve this answer



Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *