Hi guys,
In his excellent book, 55 Specific ways to improve your programs and designs, Scott Meyers has the following to say about dynamic casting:
1.) "If you find yourself wanting to cast, it’s a sign that you could be approaching things the wrong way. This is especially the case if your want is for dynamic_cast." pg 120
2.) "many implementations of dynamic_cast can be quite slow" pg 120
3.) "Good C++ uses very few casts" pg 123
4.) "you should be especially leery of dynamic_casts in performance-sensitive code." pg 121
One of Scott's solutions is the following: "provide virtual functions in the base class that let you do what you need"
As I have up to this point relied heavily on dynamic_casts (especially the cascading types) I was wondering, given the following scenario...
class Person{};
class Student:public Person{};
class Employee:public Person{};
//later in the code
std::list<Person*> people;
for(auto it=people.begin();it!=people.end();it++){
if((*it)->type==STUDENT_TYPE){
//need to access student id number, classes etc.
}
if((*it)->type==EMPLOYEE_TYPE){
//need to access social insurance number
}
}
So my question is, how would one implement virtual functions in the "if statements", that allowed one to access data members (without dynamic_cast of course)
Thanks,
Mike