假設我有一個模板類:定義專門的模板類的構造函數時避免重複
template<typename T>
class Widget {
public:
Widget(const std::string& name, int i) : t_(name), cf_(name), ci_(i) {}
private:
T t_;
const Foo cf_;
const int ci_;
}
並假設,在此背景下,T
只會Foo
或int
。在這種情況下,除Widget<Foo>
和Widget<int>
以外的任何內容都是無稽之談。
上面聲明的構造函數適用於Widget<Foo>
。有沒有一種方法可以爲Widget<int>
定義一個專門的構造函數,該函數以t_
的分配方式不同,但是沒有複製 - 粘貼cf_
和ci_
的初始化?
在精神上有點類似Base(a)
Derived::Derived(int a, int b) : Base(a), b_(b) {}
也許吧?