"C + +" accesses hidden objects in derived classes that are inherited by the base class through pointer variables of the base class

Source: Internet
Author: User

//can assign the address of an object of a derived class to a pointer variable of its base class, but only through this pointer access a hidden object in the derived class that is inherited by the base class,//cannot access new members in the derived class. The same cannot be done in turn. #include <iostream>using namespace Std;class b{public:b () {cout<< "B" <<ENDL;} void Fun () {cout<< "B::fun ()" <<ENDL;} Private:int x;}; Class D:public B{public:d () {cout<< "D" <<ENDL;} void Fun () {cout<< "D::fun ()" <<ENDL;} void Show () {cout<< "D::show ()" <<ENDL;} Private:int y;}; void Main () {D D; B *PB = &d;pb->fun ();//Pb->d::fun (); Pb->show ();    Error, a pointer variable through a base class cannot access a new member of a derived class}

C + + accesses hidden objects in derived classes that inherit from the base class through pointer variables of the base class

Related Article

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.