如何向用户隐藏私有基类成员和方法 [英] How to hide private base class members and methods from users

查看:56
本文介绍了如何向用户隐藏私有基类成员和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我没有明确说明我的问题,但以下答案并未回答我的问题.让我让这个问题更具体.我的问题是,我有一个要发送给客户的基类,以便客户可以在其末端开发派生类.如何隐藏私有方法和成员?

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 send to clients so that clients can develop derived classes at their ends. How can I hide the private methods and members?

例如,在以下代码段中,base.h文件声明了基类,该基类提供了三个私有虚拟方法供客户端在派生类中覆盖.客户端可以覆盖任何,全部或全部.假设客户开发了一个称为"Derived"的派生类,并通过了"Derived"类.类创建者,这样我就可以在某个地方创建派生类,例如Base * p_base = new Derived()并调用p_base-> Execute()来实际调用虚拟函数DoInitialize(),DoExecute(),DoCleanUp()的客户端实现.

For example, in the following code snippets, the base.h file declares the base class which provides three private virtual methods for clients to override in the derived classes. The clients can override none, any, or all of them. Assuming a client developed a derived class called "Derived", and passed the "Derived" class creator to me so that I can create the Derived class somewhere, e.g. Base* p_base = new Derived() and call p_base->Execute() to actually call client implementations of virtual functions DoInitialize(), DoExecute(), DoCleanUp().

顺便说一句:我认为不透明的指针不起作用.

BTW: I don't think opaque pointers will work.

在Base.h文件中:

In Base.h file:

  class Base {
        public:
              Base(); 
             ~Base();
        void Execute(); 
    
        private:
        // virtual functions to be overridden by derived classes.
        virtual void DoInitialize() {}
        virtual void DoExecute() {}
        virtual void DoCleanUp() {}
    
        private: 
        // private members and functions that are intended to hide from clients
        std::vector<float> data_; 
        ....
}

在Base.cpp文件中

In Base.cpp file

Base::Execute() {
    DoInitialize();
    DoExecute();
    DoCleanUp();
}

在客户端

class Derived : public Base {
     public:
         Derived();
         ~Derived();

     private:
         // overide base class methods 
         void DoInitialize() {}
         void DoExecute() {}
         void DoCleanUp() {}
}

最后我在某处

void main() {
     Base* p = DerivedCreater(); // creater a Derived class, assumes DerivedCreater() has passed in by clients. 
     p->Execute(); // I want to call the client implementation of DoInitialize(), DoExecute(), and DoCleanUp()
}

推荐答案

方法是使实现的指针不透明.

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

然后,在 BaseImpl 中,保留指向 Base 的指针,然后根据需要调用虚函数.您将 BaseImpl.h 保留在私有包含中,并且不将其分发给库用户.

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.

请参阅: https://en.cppreference.com/w/cpp/language/pimpl

这篇关于如何向用户隐藏私有基类成员和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆