I have a trivial inheritance case following:
class Person {
public:
const char* name;
public:
Person()
: name("Unknown person") {}
Person(const char* name)
: name(name) {}
};
class Student : public Person {
public:
Student()
: Person("Unknown Student") {}
};
If I assign a child pointer to a parent pointer, it is totally legal:
Student *s = new Student();
Person *p = s;
But if I do the other way, assigning a parent pointer to a child pointer, it would cause an error:
Person *p = new Person();
Student *s = p;
The error is: invalid conversion from ‘Person*’ to ‘Student*’.
An example where this might be useful is: I have a Person class, and depends on where this person will do in life, I will transform it into Student, Teacher, etc.
Any suggestions?