Thursday 3 May 2018

Base class and derived class

/*Created 5 derived objects*/
Derived derived(5);
   
/*reference to object*/
Derived &rDerived = derived;

/*Pointer to object*/   
Derived *pDerived = &derived;

since Derived has a Base part, a more interesting question is whether C++ will let us set a Base pointer or reference to a Derived object
// These are both legal!
    Base &rBase = derived;
    Base *pBase = &derived;

A Base Class pointer can point to a derived class object. Why is the vice-versa not true without casting? Logically a base class would not have enough information of the derived class but a derived class should have the information of the base class as well. I am missing some basics here.

No comments:

Post a Comment