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