2017-08-02 72 views
0

我正在嘗試爲我使用的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。

+1

方法是繼承_but_構造函數是一種特殊情況。請看看[SO:繼承構造函數](https://stackoverflow.com/a/434784/7478597)。由於這與Poco無關,因此它看起來像是重複的。 – Scheff

+0

你不需要公共繼承嗎? – danielspaniol

+0

[繼承構造函數]可能的重複(https://stackoverflow.com/questions/347358/inheriting-constructors) – Scheff

回答

1

您可以通過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

1

的原因是構造函數不被繼承。因此,ThreadPool類中沒有構造函數,它接受兩個參數。

另一方面,當您創建Poco::ThreadPool類時,您可以自由使用它的任何構造函數。請注意,根據documentation,有兩個構造函數,每個構造函數接受可選參數,因此不需要指定完整的參數列表。

您可以使用using declaration以「繼承」的構造函數:

class ThreadPool : Poco::ThreadPool {}; 
    using Poco::ThreadPool::ThreadPool; 
}