简体   繁体   English

从模板创建对象,类型由用户输入c ++输入

[英]Create objects from template, type is entered by user input c++

I have a class which is a template. 我有一类是模板。 I want the program to ask the user what type he wants and then instantiate an object based on the type he chose. 我希望程序询问用户他想要什么类型,然后根据他选择的类型实例化一个对象。 What would be the best way to do this? 最好的方法是什么? Something like this, but it doesn't work: 像这样的东西,但是不起作用:

template <typename T> class Object {...};
cin >> type;
Object<type> newobject;

Polymorphism 多态性

it will make objects template based and dynamic based on user input: 它将使对象模板基于用户输入而动态化:

class Base
{
public:
  virtual ~Base() {};
};

template <typename T>
class Type : public Base
{
    T type;
};

int main()
{
    int i;
    cin >> i;

    Base *b;

    switch (i)
    {
    case 0: b = new Type<float>(); break;
    case 1: b = new Type<int>(); break;
    case 2: b = new Type<char>(); break;
    }

    // ...

    delete b;
}

Templates in C++ must be resolved at compile time, so what you want here is impossible to do with templates. C ++中的模板必须在编译时解析,因此此处所需的功能与模板无关。 Your best bet will probably be implementing the Factory method pattern . 最好的选择可能是实现Factory方法模式

There are at least two approaches. 至少有两种方法。

First, create an instance using an if/switch statement based on user input, then store this instance in a pointer that does not know what the particular type is. 首先,根据用户输入使用if / switch语句创建一个实例,然后将该实例存储在不知道特定类型是什么的指针中。 It can be a pointer to an abstract base class, or a type erased pointer to a generated wrapper (like what std::function does), or a pointer to the equivalent of void and a variable to say what type it is to be used later. 它可以是指向抽象基类的指针,也可以是指向已生成的包装器的类型已擦除指针(例如std::function所做的事情),也可以是指向void和等效变量的指针,以声明要使用的类型后来。 Variations of this are doable in C Java and many other languages. 可以使用C Java和许多其他语言来进行此更改。

A second way is to switch on user input, generate the object, then proceed to run code that knows the object type generated by template. 第二种方法是打开用户输入,生成对象,然后继续运行知道模板生成的对象类型的代码。 You could do this with copy paste or macros or code generation in C and other C derived languages, but this kind of in language code generation is a newish technique in that lineage. 您可以使用C和其他C派生语言中的复制粘贴或宏或代码生成来执行此操作,但是这种语言代码生成是该世系中的一种新技术。 Most similar generic code in other C derived languages is actually obscured type erasure. 其他C派生语言中最相似的通用代码实际上是模糊的类型擦除。

Template Functions 模板功能

You can call a template function conditionally. 您可以有条件地调用模板函数。 This will generate all necessary code at compile time to support all needed types, but will only use those if your if statement calls that code. 这将在编译时生成所有必需的代码以支持所有必需的类型,但是仅当if语句调用该代码时才使用那些代码。 Then when you need to reference the user-inputted type in your function you can use 'T'. 然后,当您需要在函数中引用用户输入的类型时,可以使用“ T”。

template <typename T> class Object {...};

template <typename T> void myFunc() {
    Object<T> newobject;
    //statements
}

int main() {
    cin >> type;
    if (type == "int") myFunc<int>();
    if (type == "double") myFunc<double>();
    return 0;
}

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

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