c++ - 隐藏包含父库

标签 c++ inheritance shared-libraries

我正在编写一个依赖于另一个库(称为 ParentLib)的共享库(称为 MyLib)。 ParentLib 有一些虚函数,我在 MyLib 中实现了这些虚函数以及其他几个独立的实现。

// MyLib.h
#include <parentlib_library.h>

class Foo : public ClassinParent
{
   public:
      void DefinitionofParentLibFunction();
   private:
      // ...    
};

我能够毫无问题地编译和生成 MyLib,但是当应用程序使用 MyLib 时,我需要包含 ParentLib_library.h 来编译代码。 我的要求之一是 ParentLib 应该对应用程序完全隐藏。我不确定实现这一目标的下一步。 有什么想法吗?

最佳答案

如果您的声明用于回调或来自 3dparty lib 的接口(interface)实现 - 那么没办法。在所有其他情况下,我通常采用以下 3 种方法。

1) 使用聚合。将 ClassInParent 声明为 forward 并作为 Foo 的成员使用:

 class ClassInParent;//forward declare
 class Foo
 {
    ClassInParent* _inst; //use either pointer of reference to external
 public:
     void method_of_ClassInParent() //make facade for parent methods if needed
 }

2) 将您的类分成接口(interface)(不依赖于 ClassInParent)和实现(不通过 #include 公开)

你的 Foo.h:

class Foo
{
public:
   virtual void do_smth() = 0;
};

你的 Foo.cpp:

#include <parentlib_library.h>
class FooImpl : public Foo, public ClassInParent
{
     void do_smth()
     {//implementation 

3) 使用模板。而不是显式继承使用模板:

template <class T>
class Foo : public T
{

稍后在您的代码中 Foo<ClassInParent>

关于c++ - 隐藏包含父库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19375395/

相关文章:

c - 覆盖函数指针后面的c静态函数

c - 以两种不同方式指向动态库的函数指针

c++ - 从 dll/共享库返回指针会导致段错误

c++ - 从客户端接收消息时如何解决窗口卡住问题(无响应)?

c++ - 'const' 是否取消了通用引用的资格?

Windows 服务中可能使用 C++ SendInput 吗?

c++ - C++派生类访问基类的好友运算符

c++ - 来自基类的函数被隐藏在子类中。我该如何解决?

c++ - 提供用户定义的移动构造函数时,Visual Studio 2013 不会删除复制构造函数

ruby-on-rails - 查找后的 Mongoid 文档持久性