How to hide private base class members and methods from users

后端 未结 1 872
陌清茗
陌清茗 2021-01-29 11:38

Maybe I didn\'t make my question clear, the following answers are not answering my question. Let me make the question more specific. My question is that I have a base class to s

相关标签:
1条回答
  • 2021-01-29 12:08

    The way to go is to have an opaque pointer to the implementation.

    class BaseImpl;
    
    class Base {
    public:
      Base(); 
     ~Base();
    
    private:
    // virtual functions to be overridden by derived classes.
    virtual void Initialize() {}
    
    private: 
    
    // private members and functions that are not intended to override by derived classes
    void Configure() { m_impl->Configure(); }
    BaseImpl* m_impl;
    }
    

    Then, in the BaseImpl, you keep a pointer to the Base and you call the virtual functions as wanted. You keep BaseImpl.h in your private includes and you don't distribute it to the library users.

    See: https://en.cppreference.com/w/cpp/language/pimpl

    0 讨论(0)
提交回复
热议问题