C++ One std::vector containing template class of multiple types

前端 未结 5 769
太阳男子
太阳男子 2021-01-31 02:03

I need to store multiple types of a template class in a single vector.

Eg, for:

template 
class templateClass{
     bool someFunction()         


        
5条回答
  •  梦毁少年i
    2021-01-31 02:41

    Why your code doesn't work:

    Calling a virtual function on a value doesn't use polymorphism. It calls the function which is defined for the type of this exact symbol as seen by the compiler, not the runtime type. When you insert sub types into a vector of the base type, your values will be converted into the base type ("type slicing"), which is not what you want. Calling functions on them will now call the function as defined for the base type, since not it is of that type.

    How to fix this?

    The same problem can be reproduced with this code snippet:

    templateInterface x = templateClass(); // Type slicing takes place!
    x.someFunction();  // -> templateInterface::someFunction() is called!
    

    Polymorphism only works on a pointer or reference type. It will then use the runtime type of the object behind the pointer / reference to decide which implementation to call (by using it's vtable).

    Converting pointers is totally "safe" with regard to type slicing. Your actual values won't be converted at all and polymorphism will work as expected.

    Example, analogous to the code snippet above:

    templateInterface *x = new templateClass();  // No type slicing takes place
    x->someFunction();  // -> templateClass::someFunction() is called!
    
    delete x;  // Don't forget to destroy your objects.
    

    What about vectors?

    So you have to adopt these changes in your code. You can simply store pointers to actual types in the vector, instead of storing the values directly.

    When working with pointers you also have to care about deleting your allocated objects. For this you can use smart pointers which care about deletion automatically. unique_ptr is one such smart pointer type. It deletes the pointee whenever it goes out of scope ("unique ownership" - the scope being the owner). Assuming the lifetime of your objects is bound to the scope this is what you should use:

    std::vector> v;
    
    templateClass *i = new templateClass();    // create new object
    v.push_back(std::unique_ptr(i));  // put it in the vector
    
    v.emplace_back(new templateClass());   // "direct" alternative
    

    Then, call a virtual function on one of these elements with the following syntax:

    v[0]->someFunction();
    

    Make sure you make all functions virtual which should be possible to be overridden by subclasses. Otherwise their overridden version will not be called. But since you already introduced an "interface", I'm sure you are working with abstract functions.

    Alternative approaches:

    Alternative ways to do what you want is to use a variant type in the vector. There are some implementations of variant types, the Boost.Variant being a very popular one. This approach is especially nice if you don't have a type hierarchy (for example when you store primitive types). You would then use a vector type like std::vector>

提交回复
热议问题