c++ 只继承 public 个构造函数

c++ Only inherit public constructors

我试图从基础 class 继承构造函数,但出现错误:C2876:'Poco::ThreadPool':并非所有重载都可访问。

namespace myNamespace{

    class ThreadPool : public Poco::ThreadPool{
        using Poco::ThreadPool::ThreadPool;  // inherits constructors
    };

}

Poco::ThreadPool 有 3 个构造函数,2 个 public 都带有默认初始化参数,还有 1 个私有构造函数。

如何只继承public个构造函数?

我没有使用 c++11。

如果您不使用 C++11 或更高版本,则无法通过单个 using 声明继承所有基类构造函数。

C++11 之前的旧方法是在派生的 class 中为我们想要公开的基中的每个 c'tor 创建一个对应的 c'tor。例如:

struct foo {
  int _i;
  foo(int i = 0) : _i(i) {}
};

struct bar : private foo {
  bar() : foo() {} // Use the default argument defined in foo
  bar(int i) : foo(i) {} // Use a user supplied argument
};

如果您仍然想要一个带有默认参数的 c'tor,您也可以这样做:

struct baz : private foo {
  baz(int i = 2) : foo(i) {} // We can even change what the default argument is
};