[C++]One std vector containing template class of multiple types
keywords: vector, list, element, multiple template types, different types
How to stroe elements with different types in one vector?
template define:
class templateInterface
{
virtual bool someFunction() {return true;}
};
template <typename T>
class templateClass : public templateInterface
{
bool someFunction() {return false;}
};
setup vector elements:
std::vector<std::unique_ptr<templateInterface>> v;
templateClass<int> *i = new templateClass<int>(); // create new object
v.push_back(std::unique_ptr<templateInterface>(i)); // put it in the vector
v.emplace_back(new templateClass<int>()); // "direct" alternative
call a virtual function on one of these elements:
v[0]->someFunction();
愿余生有人鲜衣怒马,陪你看烈焰繁花 ;
愿余生有人素面白纱,陪你度恬淡年华 。