C++ 制作模板类的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18362333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:55:50  来源:igfitidea点击:

Making an object of template class

c++qttemplatesobject

提问by Mike

I have a template class defined :

我定义了一个模板类:

 template <class T>
    class TempClass
    {
    ...
    };

If I want to make an object of this class in let's say some MyClass.h to access functions of template class, how shall I pass the argument of the template? I tried to do the following:

如果我想在让我们说一些 MyClass.h 中创建这个类的对象来访问模板类的函数,我应该如何传递模板的参数?我尝试执行以下操作:

class MyClass{
public:
    TempClass<T> temp;
}

Sure, as supposed it does not work, as T is not defined in MyClass, so I am a bit lost how do it correctly.

当然,假设它不起作用,因为 T 没有在 MyClass 中定义,所以我有点迷失如何正确地做到这一点。

Thanks.

谢谢。

采纳答案by Adam H. Peterson

If you want MyClassto be a template as well, you would do it like this:

如果你也想MyClass成为一个模板,你会这样做:

template<typename T>
struct MyClass {
    TempClass<T> temp;
};

(You could also use classinstead of struct, but since all members are public, you don't really need default private.)

(您也可以使用class代替struct,但由于所有成员都是公开的,因此您实际上并不需要默认的私有。)

If you don't want MyClassto be a template, you will need some concrete type to substitute in for T. For example:

如果你不想MyClass成为一个模板,你将需要一些具体的类型来代替T. 例如:

struct MyClass {
    TempClass<string> temp;
};

Pedantic:Technically TempClassisn't a template class, it's a class template. A class template isn't actually a class, it's a template that can be used to create individual classes that are themselves template classes. Thus, TempClassis a class template, while TempClass<string>is a template class --- a class that is created by instantiating a template.

Pedantic:技术上TempClass不是模板类,它是类模板。类模板实际上并不是一个类,它是一个模板,可用于创建本身就是模板类的各个类。因此,TempClass是类模板,而TempClass<string>是模板类——通过实例化模板创建的类。

回答by Amadeus

You can instantiate your template like this, for example, to instantiate for int:

你可以像这样实例化你的模板,例如,为 int 实例化:

class MyClass
{
public:
  TempClass<int> temp;
};

But if you still want you MYClass to be generic, you can make it template too and define it like this:

但是如果你仍然希望你的 MYClass 是通用的,你也可以将它设为模板并像这样定义它:

template<typename T>
class MyClass
{
public:
  TempClass<T> temp;
};

and let the any MyClass object instantiation to define parameter T, for example:

并让任何 MyClass 对象实例化来定义参数 T,例如:

MyClass<int> class;

回答by Manu343726

Templates are not classes. Are. as it name means, templates wich helps the compiler to create classes. That is, if you have a template class template<typename T> class Foo{};:

模板不是类。是。顾名思义,模板可帮助编译器创建类。也就是说,如果您有一个模板类template<typename T> class Foo{};

template<typename T>
struct Foo
{
    T attribute;
};

Its only a template which the compiler uses to generate different versions of Foo, each for a specified type. When you instantiatea template, that is, tells the compiler you need that class generated with a specified type, the compiler generates a version of Foocode replacing the template argument with the specified type:

它只是编译器用来生成不同版本的模板Foo,每个版本都用于指定的类型。当您实例化模板时,即告诉编译器您需要使用指定类型生成该类,编译器生成一个Foo代码版本,用指定类型替换模板参数:

int main()
{
    Foo<int> foo_int_variable;
    Foo<bool> foo_bool_variable;
}

The compiler after seeing that two instances, generates code like this:

编译器看到这两个实例后,生成如下代码:

struct __Foo_int
{
    int attribute;
};

struct __Foo_bool
{
    bool attribute;
};

So the code of main is translated to this:

所以main的代码翻译成这样:

int main()
{
    __Foo_int foo_int_variable;
    __Foo_bool foo_bool_variable;
}

So the answer is: You need to specify what type you need, to let the compiler to generate the correct template instantiation.
If the class that uses Foo, like in your example, don't need a specific instantiation of Foo, needs a generic version of Foo, you could make that class a template too.

所以答案是:你需要指定你需要什么类型,让编译器生成正确的模板实例化
如果使用 的类Foo(例如在您的示例中)不需要 Foo 的特定实例化,而需要 的通用版本Foo,则您也可以将该类设为模板。