C++ template class design with varying method return types -


c++ design question

simplified example

template <typename t> class thing {     t value; public:     t get() { return value; } };  template <typename t> class box {     thing<t> thing; public:      t get() { return thing.get(); } }; 

now need store different types of box,

box<string>,  box<matrix>,  etc  

(where none of t classes related) in container. isn't possible, since t isn't same each type of box. can't create abstract superclass since return type of t varies , there nothing in get() parameters distinquish use, if somehow provide alternative implementations.

how solve reasonable way. sure add multiple get()s in abstract class doesn't seem c++ ish. @ point have check isthisastring, isthisamatrix, question what's reasonable design, assuming might need add specialized box subclasses in future still maintain set of methods similar get() based on template parameter.

what looking called "type erasure". there several implementations :


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -