How can I hide a class in C++?

后端 未结 4 590
旧时难觅i
旧时难觅i 2021-01-11 23:49

Let\'s say I have 2 classes that I want to be visible (within a given header file) and one class that is their ancestor, which one I want to be visible only to the previousl

4条回答
  •  北海茫月
    2021-01-12 00:26

    A variation of Anonymous answer, instead of private inheritance you can add private member of the hidden class.

    class Hidden
    {
       private:
          friend class Exposed;
          Hidden() {}
          int hidden_x;
    };
    
    class Exposed
    {
      public:
          Exposed() {}
          void DoStuff() { printf( "%d" , hidden.hidden_x ); }
      private:
          Hidden hidden_;
    };
    

提交回复
热议问题