1, reference base class object directly with base class pointer
2, referencing derived class objects directly with derived class pointers
3, reference a derived class object with a base-class pointer, because the derived class object is also an object of the base class, so the reference is safe,
However, only base class members can be referenced. If you attempt to reference a member that is only in a derived class by using a base-class pointer, the compiler reports a syntax error. (The answer to this question is virtual functions and polymorphism)
4, reference the object of the base class with a derived class pointer. This type of reference can lead to syntax errors. Derived class pointers must first be cast to a base class pointer, which is unsafe.
And in the Houtie of the second chapter of the main character of C + + in MFC:
1, if you point to a "base class pointer" to a "derived class object", then through the pointer you can only call the function defined by the base class
2. If you point to a "base class object" with a "pointer to a derived class", you must first make a significant transition (explicit cast), which is dangerous.
3, if both the base class and the derived class define the "function of the same name", then when invoking a member function through an object pointer, the function must be called, depending on the original type of the pointer, rather than depending on the type of object the pointer actually refers to, which is in fact connected with the 1th meaning.
Defines a parent class object that casts a pointer to a child class pointer to the parent class object, and assigns a pointer to the child class, the child class pointer will point to the parent class object, but its this pointer is still a pointer to the child class.
When the pointer calls a virtual member function, the parent class virtual member function is called according to the object's memory layout (the first four bytes of the object are stored in the pointer to the virtual function table).
When the pointer calls a normal member function, the member function of the subclass that is located in the code snippet is called through the this pointer.
Sub-class pointers to C + + point to parent class