C++ DLL:不暴露整个类

Fir*_*cer 0 c++

如何"隐藏"类的某些部分,以便使用该库的人不必包含我班级中使用的所有类型的标题.即下面的MainWindow类,我可以拥有它,所以当在静态/动态库中编译时,无论谁使用libary都不必包含windows.h,即HWND,CRITICAL_SECTION,LRESULT等不必定义.

我知道我可以把它分成两个类,一个只有公共接口的抽象类,一个隐藏的实现类,它包含需要windows.h的成员.

这里的问题是无法再创建可见类,并且需要额外的创建函数(例如CreateMainWindow).在这种情况下,这很好,因为最有可能只需要在堆上创建的单个实例,但对于其他类,则不是这样.

class MainWindow
{
    HWND hwnd;
    int width, height;
    std::string caption;
    bool started,exited;
    bool closeRequest;

    unsigned loopThread;
    CRITICAL_SECTION inputLock;

    Input *input;
public:
    static void init_type();
    Py::Object getattr(const char *name);

    MainWindow(int width, int height, std::string caption);
    ~MainWindow();

    bool CloseRequest(const Py::Tuple &args);
    bool CloseRequestReset(const Py::Tuple &args);

    HWND GetHwnd();

    int GetWidth();
    int GetHeight();

    Input* GetInput();
protected:
    unsigned static __stdcall loopThreadWrap(void *arg);
    unsigned LoopThreadMain();

    LRESULT WndProc(UINT msg, WPARAM wParam, LPARAM lParam);
    LRESULT static CALLBACK WndProcWrapper(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
};
Run Code Online (Sandbox Code Playgroud)

Chr*_*isW 7

您可以使用所谓的"cheshire cat","letter/envelope"或"pimpl"技术隐藏类的一部分(对于相同的技术,它们都是不同的名称):

class MainWindow
{
private:
    //opaque data
    class ImplementationDetails;
    ImplementationDetails* m_data;
public:
    ... declare your public methods here ...
}
Run Code Online (Sandbox Code Playgroud)

最好的方法可能是你的第二段中提到的抽象类(但是我​​无法理解你的最后一句话,你在其中(尝试/未能)解释你的反驳论点是什么).