2011-02-11 60 views

回答

2

這是可能的,但只有當您定義some的實現,否則您將面臨編譯錯誤。您可能還需要添加一個受保護的構造函數,以便您的基類不能在您的頭範圍內定義之外創建和使用。

template<typename T> 
class TestParent{ 
    public: 

     T* some() { return new T(); } 

    //this is suggested 
    protected: 
     TestParent(){} 
}; 

class TestChild : public TestParent<TestChild>{} 

這在curiously recuring template pattern使用和policy-based design其他技術通過Alexandrescu的的book流行起來。

+0

+1爲鏈接,非常有用 – Gerard 2014-05-08 11:04:39