Conversion between parent class subclass pointers

Source: Internet
Author: User

1. When your class Pointer Points to the object of your class, whether it is a virtual function or a real function, it calls its own:

2. When the parent class pointer pointing to the parent class object is forcibly converted to a subclass pointer, when the subclass pointer calls a function, only the non-override function is its own, and the virtual function is its parent class;

3. when the subclass pointer pointing to the subclass object is forcibly converted to the parent class pointer, that is, the parent class Pointer Points to the subclass object, at this time, the virtual functions called by the parent class pointer are all subclasses, rather than virtual functions;

 

Summarize the above three sentences into one sentence:When the parent class subclass has a non-virtual function with the same name, it calls a function of its own type;

When the parent class subclass has a virtual function with the same name, it calls the object type function pointed to by the pointer.

For details, see the following code:

# Include <iostream> using namespace STD; class base {public: Virtual void F () {cout <"base: F" <Endl;} virtual void g () {cout <"base: G" <Endl ;}void H () {cout <"base: H" <Endl ;}}; class derived: public base {public: Virtual void F () {cout <"derived: F" <Endl;} void g () {cout <"derived :: G "<Endl;} void H () {cout <" derived: H "<Endl ;}}; void main () {base * pb = new base (); cout <"when the base class Pointer Points to the Base Class Object:" <Endl; Pb-> F (); pb-> G (); Pb-> H (); cout <Endl; derived * Pd = (derived *) Pb; cout <"when the parent class pointer is forcibly converted to a subclass pointer:" <Endl; Pd-> F (); Pd-> G (); pd-> H (); cout <Endl; derived * Pd = new derived (); cout <"when the subclass Pointer Points to the subclass" <Endl; pd-> F (); Pd-> G (); Pd-> H (); cout <Endl; base * pb = (base *) PD; cout <"when the subclass pointer is forcibly converted to the parent class pointer:" <Endl; Pb-> F (); Pb-> G (); pb-> H (); cout <Endl; base * PP = new derived (); cout <"when the parent class Pointer Points to a subclass object:" <Endl; PP-> F (); PP-> G (); PP-> H (); cout <Endl ;}

The execution result is as follows:

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.