c++ - Templated data types with derived classes -
let's have following:
class base { protected: base() { } }; class : public base { }; class b : public base { };
now suppose template:
templatedclass<base> *generic = new templatedclass<a>();
it doesn't work, , believe understand why, i'd know if can equivalent. have several template specializations of form
typedef templatedclass<a> aspec; typedef templatedclass<b> bspec; typedef templatedclass<c> cspec;
i have single variable type i'd defer until runtime, can dynamically assign like
if(condition1) generic = new aspec(); else if(condition2) generic = new bspec();
is there way go this? don't have ability change fact classes templated , not inheriting base class, or i'd that.
this not possible in c++. fact a
derives base
doesn't mean templatedclass<a>
derives templatedclass<base>
.
see stack overflow post alternatives: conversion stl vector of subclass vector of base class
Comments
Post a Comment