I have an object, and it is stored as a unique_ptr of the base class in a map.
class Base
{
int foo;
};
map <string, unique_ptr<Base> > Objects;
however, the objects are actually of different child classes.
class Child1 : public Base
{
int bar1;
};
class Child2 : public Base;
{
int bar2;
};
Objects.insert(make_pair("Child1", unique_ptr<Base>(new Child1())));
Objects.insert(make_pair("Child2", unique_ptr<Base>(new Child2())));
Now, somewhere far away in the code, I want to access a member variable of Child1, which is unique to that class.
cout << "Bar1 is:" << Objects["Child1"]->bar1 << endl;
But I get the error:
ERROR: 'class Base' has no member 'bar1'
So I try to use dynamic_cast, but of course, you cannot use dynamic_cast on a unique_ptr.
So what can I do?