I understand that friend
is not inherited. I have classes Parent Person Child
. Parent and Person are friends of each other. Parent has the following PROTECTED function...
class Person
{
friend class Parent;
public:
Parent * parent;
}
class Parent
{
friend class Person;
protected:
virtual void Answer() = 0;
}
class Child : public Parent
{
void Answer()
{
std::cout << "Child!" << endl;
}
}
My question is, if friendship is not inherited, how am I able to have the following...? (Person has a pointer to Parent)
Person person;
Child child;
person.parent->Answer();
Why is the output of this Child!, and it does not throw a runtime error when trying to access a virtual function?
I am confused as to how the child's function is being implemented, and the program is not erroring on run time since I would anticipate that it is trying to call Parent's virtual Answer function.