I'm a beginner with c++ :), I simplified my problem to this:
I have a class Person
class Person{
string name;}
And another class Student that inherits Person
class Student : public Person{
string schoolName;}
And then I have a class Library that must have a different comportement depending on the input
class Library{
void checkBeforeEntry(Person* p){
cout << "You must be a student to enter" << endl;}
void checkBeforeEntry(Student* s){
cout << "You can enter" << endl;}}
At some point in my programm I'm storing a person like that: Person* guy;
because at that point I don't know yet if he is a simple person or a student.
But due to that I'm "loosing" the information about the real type of the object and I have tried this:
Person *p = new Person();
Student *s = new Student();
Person *g = new Student();
lib.checkBeforeEntry(p);
lib.checkBeforeEntry(s);
lib.checkBeforeEntry(g);
The Output was:
You must be a student to enter
You can enter
You must be a student to enter
But I want the last statement to be "you can enter". I used a bit of polymorphism before to adapt the comportement of the derived class but here I'm trying to change the comportement of a third party class (library) so the situation is not the same as I am used to. Do you know how to solve this or if I should use another design ?
Aucun commentaire:
Enregistrer un commentaire