简体   繁体   English

用C ++方法创建模板类?

[英]Create template class in C++ method?

How can I do the following, without having to include every class T in this file?: 如何在不必在此文件中包含每个类T的情况下执行以下操作?:

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

Basically, I want a generic class instantiater. 基本上,我想要一个泛型类实例化器。 How can I achieve this without including headers everywhere? 如何在不包含标题的情况下实现此目的?

You do it the other way round; 你反过来这样做; you include "ComponentMan.h" everywhere you want to use it. 在任何想要使用它的地方都包含“ComponentMan.h”。 eg: 例如:

foo.h foo.h中

class Foo {
    ...
};

blah.cpp blah.cpp

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM