In general, if you want polymorphism, you would use a base class:
class GVarBase
{
public:
virtual ~GVarBase() {}
};
template <typename T>
class GVar: public GVarBase
{
public:
private:
T mVar;
};
std::stack< std::unique_ptr<GVarBase> > stack;
Note that with the current code that you have, std::stack< GVar<int> >it wonβt even work, a default constructor is required.
source
share