问题
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