Sub-class pointers to C + + point to parent class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.