I\'m wondering if it\'s possible to create a map of pointers of inherited classes. Here\'s an example of what I\'m trying to do:
#include
#inc
First things first:
template
map m;
This is not valid C++ and could only mean something like a template alias
, but I believe, that is not what you are looking for.
Storing polymorphic objects in C++ is complicated by slicing (constructing a value of the base type from a value of a derived type). Dynamic polymorphism can only be handled through references or pointers. You could potentially use std::ref
or boost::ref
for situations in which the map will only be passed down the callstack, but this requires some care. Often, storing pointers to the base is the way to go: std::map
. Managing deallocation yourself is rather tedious and either std::map
or std::map
are preferred, depending if you need shared semantics or not.
Basic example. Someone should replace this with something more meaningful.
#include
#include
#include