How can I declare a template pointer without knowing the type?

后端 未结 3 1476
孤城傲影
孤城傲影 2021-02-01 19:27

This is what I would like to do:

ExampleTemplate* pointer_to_template;
cin >> number;
switch (number) {
case 1:
    pointer_to_template = new ExampleTempla         


        
3条回答
  •  半阙折子戏
    2021-02-01 20:16

    What you're trying to do is not possible. This is becase your ExampleTemplate class doesn't exist by itself, only exists when you relate it with a type.

    You could get that behaviour using inheritance:

    1. Define a GeneralExampleTemplate (not a template class).
    2. Make ExampleTemplate inherit from GeneralExampleTemplate.
    3. That way you can create a GeneralExampleTemplate pointer and assign it with a (for example) ExampleTemplate.

提交回复
热议问题