C + + class inheritance Five (ambiguity of multiple inheritance--Virtual base class)

Source: Internet
Author: User

//ambiguity of multiple inheritance--Virtual base class (knowledge-based)#include <iostream>using namespacestd;/*multiple inheritance is generally not used in current project development, he will increase the complexity of the project*/classpoint{ Public: Point () {x=1; Y=1; }    intx; inty;};classPointa:Virtual  Publicpoint{ Public: Pointa () {a=2; }    intA;};classPOINTB:Virtual  Publicpoint{ Public: Pointb () {b=3; }    intb;};classPOINTC: PublicPointa, Publicpointb{};voidProtecta () {POINTC pc1; //pc1.x = 1; Error C2385: Access to "X" is ambiguous//At this point we can use the virtual keyword to decorate the inheritance relationshipPC1.Y =2; //The virtual keyword can detect that both Pointa and POINTB are inherited from point, so pc1.y = 2; is assigning a value to a point class object}voidMain () {protecta (); System ("Pause");}

C + + class inheritance Five (ambiguity of multiple inheritance--Virtual base 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.