While using virtual function, usually we use base class pointer to access derived class object. can we use derived class pointer to access base class object. if no, what is the reason?
Printable View
While using virtual function, usually we use base class pointer to access derived class object. can we use derived class pointer to access base class object. if no, what is the reason?
Hi,
There is nothing wrong in doing it the way you said...You won't even get any compile or run-time error. But the problem is you won't get the output that you have anticipated because "object slicing" occurs whenever you perform "downcasting"( referring to the base class thru a derived class). Because construction of an object always occurs from "top down" in a hierarchy.