Search code examples
c++friendnested-class

C++ and friendship of nested private classes


I have 2 classes like the dummy one below

   class CA
   {
   private:
    class Impl;
   };

   class Cb
   {
   private:
    friend class CA::Impl;
   }

This code is giving me compilation error saying that class CA::Impl is private. Is it really not possible to put in place friendship for nested private classes? What else can I do to implement this semantic?


Solution

  • You, Adam, and Steve

    Imagine that there is this celebrity named Adam Stackie, who has a friend named Steve.

    Wouldn't it be weird if you by knowing certain things about Adam, even though you are not his friend, just assumed that you and Steve were close enough for you to grab him in public?

    A class is not implicitly a friend of a friend, nor is it implicitly a friend of something which it can reach into certain parts off.


    A little bit more serious

    To make your snippet work you will either have to

    • Make Cb a friend of CA, by putting in a forward- and a friend-declaration for Cb in CA , or;
    • make CA::Impl public, or;
    • in any other way make sure that Cb can grab onto CA::Impl.