Assignment-compatible with the principle of

In C ++, no matter under what inheritance derived class the parent class will inherit all of the data, the difference is whether it can access
assignment-compatible with the following rules:
1. directly to the base class object populated with the derived class object
2 can be directly derived address of the class object pointer assignment to the base class
3 may be used to reference the derived object to initialize the base class directly
, but the principle can not be run in reverse, willing follows:
to mobilize constructor when instantiating a type, configuration and subclass before the first parent object constructor, the compiler in advance to open up space plus the total size of a subclass of the parent class. Parent members after completion of the first into the structure, and then add a subclass members constructed in the back, and inheritance are all members together constitute a subclass object. Subclass can be assigned to the parent class is a subclass because the parent class members include a member of the assignment just uses the parent class and the same space (called the target slice), the parent class members what is required, subclasses can all provide . And in turn, the parent can not provide all of the members of the subclasses need.

Guess you like

Origin blog.51cto.com/14289397/2478117