Assigning values between a base-class pointer and a child-class pointer
(1) When assigning a subclass pointer to a base class pointer, no forced type conversion is required, and the C + + compiler automatically casts the type. Because the child class object is also a base class object.
(2) When assigning a base-class pointer to a child-class pointer, coercion of type conversions is required, and the C + + compiler does not automatically type-convert. Because the base class object is not a subclass object. The self-increment part of a subclass object is not a base class. (casting tells the compiler to increase the portion of the subclass that is unique to the object)
fish* fh1; animal* an1 = new Animal; FH1 = (fish*) an1;
Principle:
When we construct the object of the fish class, we first call the constructor of the animal class to construct the constructor of the animal class, and then call the fish class's constructor to complete the construction of its own part, thus stitching up a complete fish object.
Storage of fish objects in memory
Animal the memory of the object |
Fish Inheritance Section |
When we convert a fish class object to a animal class object, the object is considered the upper half of the entire memory model of the original object, which is the memory portion of the animal object in the diagram. When we use a type-converted object Pointer to invoke its method, it is natural to call the method in memory where it resides.
Polymorphic
The real difference between polymorphic and non-polymorphic is whether the function address is early bound or late bound (polymorphic). If a function is called, the call address of the function can be determined during compiler compilation, and the production code is static, which means that the address is early bound. If the address of a function call cannot be determined during the compiler, it needs to be determined at run time, which is a late binding.
The most common usage is to declare a pointer to a base class, use it to point to any subclass object, invoke the corresponding virtual function, and implement a different method depending on the sub-class to which it is directed. If you do not use a virtual function, that is, you are not using C + + polymorphism, when you invoke the corresponding function using the base class pointer, it will always be limited to the base class function itself, and cannot be called to a function that has been overridden in a subclass.
code Form for virtual functions for non-virtual functions
Action binding mode Action binding mode
Class Name:: Function () invokes the specified function of the specified class static binding invokes the specified function of the specified class
The object name. function () invokes the specified function of the specified object static binding invokes the specified function of the specified object
A reference variable. function () invokes the specified function of the class to which the referenced object belongs. Dynamic binding invokes the specified function of the class to which the reference variable belongs static binding
pointer-to-function () invokes the specified function of the class to which the referenced object belongs dynamically binding the specified function of the calling pointer variable to the class to which it belongs
As can be seen from the table above, the only way to perform dynamic binding is through an address, that is, only through pointers or reference variables, and it must also be a virtual function. Conceptually, a virtual function mechanism works only when applied to an address, because the type information provided by the address during the compile phase is not complete.
C + + base class pointers, sub-class pointers, polymorphic