从基类继承函数声明中参数的默认值
问题描述:
我正在尝试为我使用的Poco函数编写包装类。从基类继承函数声明中参数的默认值
这里是我的代码:
//PocoWrapper.h
#include <stdio.h>
#include <Poco/Task.h>
#include <Poco/TaskManager.h>
#include <Poco/ThreadPool.h>
namespace myNamespace{
class ThreadPool : Poco::ThreadPool{};
}
现在,如果我有PocoWrapper.h在另一个剧本,我应该能够使用:
myThreadPool = new myNamespace::ThreadPool(1,4);
但是这给了错误:
//error: C2661: 'myNamespace::ThreadPool::ThreadPool' : no overloaded function takes 2 arguments
但是,如果我使用:
myThreadPool = new Poco::ThreadPool(1,4);
它编译得很好。因此,问题一定是它不会继承Poco :: ThreadPool类中的函数的默认值。
ThreadPool构造函数具有默认值,所以它应该只能使用2个参数。从documentation:
ThreadPool(
int minCapacity = 2,
int maxCapacity = 16,
int idleTime = 60,
int stackSize = 0
);
我怎样才能让我的包装类的工作只有两个参数,如基类呢?
我没有使用C++ 11。
答
您可以通过using
他们集体的名义继承基类的构造函数:
namespace Poco {
struct ThreadPool{
ThreadPool(int);
ThreadPool(int,int);
};
}
namespace myNamespace{
class ThreadPool : Poco::ThreadPool{
using Poco::ThreadPool::ThreadPool; // inherits constructors
};
}
+0
谢谢,但我现在得到错误:“错误C2876:Poco :: ThreadPool:并非所有重载都可访问”。我怎样才能继承公共构造函数? – Blue7
答
的原因是构造函数不被继承。因此,ThreadPool
类中没有构造函数,它接受两个参数。
另一方面,当您创建Poco::ThreadPool
类时,您可以*使用它的任何构造函数。请注意,根据documentation,有两个构造函数,每个构造函数接受可选参数,因此不需要指定完整的参数列表。
您可以使用using declaration以“继承”的构造函数:
class ThreadPool : Poco::ThreadPool {};
using Poco::ThreadPool::ThreadPool;
}
方法是继承_but_构造函数是一种特殊情况。请看看[SO:继承构造函数](https://*.com/a/434784/7478597)。由于这与Poco无关,因此它看起来像是重复的。 – Scheff
你不需要公共继承吗? – danielspaniol
[继承构造函数]可能的重复(https://*.com/questions/347358/inheriting-constructors) – Scheff