2011-12-17 108 views
0

我怎麼能做到以下幾點,而不必包含在此文件中的每個T類?:在C++方法中創建模板類?

// ComponentMan.h 
class ComponentMan 
{ 
public: 
    template<class T> 
    void CreateComponent<T>() 
    { 
     T* temp = new T(); 
    } 
} 

基本上,我想一個通用類instantiater。如何在不加入標題的情況下實現這一目標?

回答

2

你這樣做;您在任何想要使用它的地方都包含「ComponentMan.h」。例如: -

foo.h中

class Foo { 
    ... 
}; 

blah.cpp

#include "foo.h" 
#include "ComponentMan.h" 

void bar() { 
    ComponentMan man; 
    man.CreateComponent<Foo>(); 
} 
+0

是謝謝你這個作品! – NomenNescio 2011-12-17 16:42:42