template class containing a stdvector of references to its own instances

吃可爱长大的小学妹 提交于 2019-12-24 09:15:56

问题


I'm relatively new to C++, and am trying for the first time to build a complex template structure.

How can I declare, as member of a template class Foo, a std::vector of Foo* elements, but that could be of various types?

#include <vector>

template <typename T>
class Foo {
    T mValue;
    std::vector< Foo<T>* > mFooParameters;  // <---- I would like this vector to contain
                                            //       any sort of Foo<T>* elements,
                                            //       Foo<int>*, Foo<double>*, etc.
};

Is it straightforward, possible but complicated, or impossible?

Thank you for your answers!


回答1:


If your vector types are all related, use polymorphism as explained in the link provided by @jogojapan, if the types are not related at all, use a vector of void* to hold pointers to your data (kind of messy though)



来源:https://stackoverflow.com/questions/12967174/template-class-containing-a-stdvector-of-references-to-its-own-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!