I have a base class
class Person{
public:
Person(string name , int age ){
this -> name = name;
this -> age = age;
}
virtual void getInfo(){
cout << "Person " << name << " " << age;
}
void add(string name , const Person & b){
a[name] = b
}
protected:
string name;
int age;
map<string , Person > a;
};
That contains map of object type Person. I want to push various derived classes into that map e.g
Derived class
class Kid : public Person{
public:
Kid(string name, int age):Person(name,age){};
virtual void getInfo( ){
cout << "Kid " << name << " " << age;
}
};
I want add
method of Person class to bahave such as
Person one("John",25);
one.add("Suzie",15);
Which fails. I know i can remake the code using pointers e.g
map<string , Person*> a
void add( string name , Person *b){
a[name] = b;
}
Person one("John",25);
one.add(new Kid("Suzie",15))
But is there a way how to achieve it without using pointers?