Function call when the parent pointer points to a child class object

Source: Internet
Author: User
Class A{public:void Funca () {     printf ("Funca called\n");} virtual void Funcb () {     printf ("FUNCB called\n");} };class b:public a{public:void Funca () {     A::funca ();     printf ("Funcab called\n"); } virtual void Funcb () {     printf ("FUNCBB called\n");}; void Main (void) {b  b; A  *pa; pa = &b; A *pa2 = new A; Pa->funca (); (3) PA->FUNCB (); (4) Pa2->funca (); (5) PA2->FUNCB (); Delete PA2;}
The parent class pointer points to the subclass instance object, and the function in the parent class is called when the normal override function is called. When you call a quilt class to override a virtual function, the function in the subclass is called. Again, the overridden virtual function in a subclass is dynamically bound, independent of the pointer type of the parent class that currently points to the class instance, and is only relevant to the class instance object itself.

Function call when the parent pointer points to a child class object

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.