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

前端 未结 5 896
太阳男子
太阳男子 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条回答
  •  半阙折子戏
    2021-01-31 02:50

    Polymorphism only works through pointers or references. You'll need the non-template base. Beyond that, you'll need to decide where the actual objects in container will live. If they're all static objects (with sufficient lifetime), just using a std::vector, and inserting with v.push_back(&t1);, etc., should do the trick. Otherwise, you'll probably want to support cloning, and keep clones in the vector: preferably with Boost pointer containers, but std::shared_ptr can be used as well.

提交回复
热议问题