[Turn]c++ the meaning and function of virtual public

Source: Internet
Author: User

I was in the process of writing based on the Mico CORBA program, I ran into the Internet.

Transferred from: http://bbs.seu.edu.cn/pc/pccon.php?id=872&nid=16822

Question:
Parent class:

class cparent{...     };


The declaration of the inheriting class is more special:

class Virtual  Public cparent{...     };


What is the function and meaning of this "virtual"?
---------------------------------------------------------------

What is the role and meaning of this "virtual"?
proves that this cparent is cchild  virtual base class
Virtual base class     function
Virtual base class refers to:class subclass : virtual  public baseclass  the base class in the virtual declaration!! Because C + + supports multiple inheritance, there are several direct parent classes in a derived class, and several of the direct parent classes may inherit from one of the base classes (that is, the parent class), so that when the final derived class is constructed, the final derived class contains multiple identical base classes. Will produce two of the semantic problem (not knowing which base class's member variables and functions to call), to solve this problem, you need to use the virtual base class, that is, only the base class to generate a piece of memory area, so that the final derived class will only contain a base class
typically need to use the virtual base class as follows:
             a
            / \
          B    c
           \ /
             d
where D inherits from BC,BC and inherits from a, so a must be inherited by BC Virtual
Program ....

 class   A { public  :  void  PrintA () {cout<< this is a\n    class  B:virtual  public   A;  class  C:virtual  public   A;  class  D:public  b,public  C; 



So after the D is constructed, there is only one a in the storage area, there will be no two semantic problems
For example: D d=new D;
If D.printa () is used at this time, there will be no problem, but if B and C are not virtual inherited from a, there are two questions of righteousness

[Turn]c++ the meaning and function of virtual public

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.