
Patrick B. answered 10/15/20
Math and computer tutor/teacher
Once again parent = child is legal, but child = parent is NOT
class B
{
public:
B() { }
};
class D : public B
{
public:
D() { }
};
int main()
{
B b;
D d;
B* pb;
D * pd;
b = d; // statement A is legal
d = b; //Compiler Error; cannot assign parent to child; Statement B is ILLEGAL !!!
pd = pb; //Compiler Error; cannot assign parent address to child address; Statement C is ILLEGAL !!!
pb = pd; // statement D is legal : assigns child address to parent address
d = pd; // statement E is ILLEGAL; type mismatch: object D vs pointer address
b = *pd; // statement F is legal; contents of child pointer can be assigned to parent object
*pd = *pb; // statement G is ILLEGAL; contents of parent pointer cannot be assigned to contents of child pointer
}