Say I have the following classes:
(I left out constructors and destructors as they don't matter here)
class Employee{
public:
virtual void pay()=0;
}
class Worker{
int age;
public:
void pay(){
cout<<"pay worker";
}
void setAge(int a){age=a;}
}
class Manager{
public:
void pay(){
cout<<"pay manager";
}
}
Now I create a map:
typedef map<string, Employee*> employees;
employees m;
and insert a few:
m.insert(pair<string, Employee*>("George",(new Worker())));
m.insert(pair<string, Employee*>("Bush",(new Manager())));
now I can use an Iterator to go through every item in the map and call:
(map_iter->second)->pay();
But, and here comes my question, how can I in a general and clean way do something similar like:
(map_iter->second)->setAge();
Certainly this should only be applied to the workers and not to the managers.
Off course you could make the function "setAge()" a virtual function in class Employee, but isn't there another way to use some kind of dynamic typechecking??
(I left out constructors and destructors as they don't matter here)
class Employee{
public:
virtual void pay()=0;
}
class Worker{
int age;
public:
void pay(){
cout<<"pay worker";
}
void setAge(int a){age=a;}
}
class Manager{
public:
void pay(){
cout<<"pay manager";
}
}
Now I create a map:
typedef map<string, Employee*> employees;
employees m;
and insert a few:
m.insert(pair<string, Employee*>("George",(new Worker())));
m.insert(pair<string, Employee*>("Bush",(new Manager())));
now I can use an Iterator to go through every item in the map and call:
(map_iter->second)->pay();
But, and here comes my question, how can I in a general and clean way do something similar like:
(map_iter->second)->setAge();
Certainly this should only be applied to the workers and not to the managers.
Off course you could make the function "setAge()" a virtual function in class Employee, but isn't there another way to use some kind of dynamic typechecking??