In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "C++ virtual inheritance example analysis", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, let's study and learn "C++ virtual inheritance example analysis"!
In multiple inheritance, if something happens, such as: class B inherits class A, class C inherits class A, class D inherits both class B and class C. Finally, there are two members of class An in class D, which cannot be tolerated in the program. Of course, the way to solve this problem is to use virtual inheritance.
1 class A {2 public: 3 int t; 4 A (int a) 5 {6 t = a; 7} 8 void fun (); 9}; 10 11 class B:virtual public A12 {13 public:14 B (int a, int b): a (int 10) 15 {16 t 1 = bscape 17} 18 ~ B (); 19 int t 1x 20} 21 22 class C: virtual public A23 {24 public:25 C (int a pencil int c): a (axiom 20) 26 {27 t 2 = c Ten 28} 29 C (); 30 int t 2t 31} 32 33 class D: public Bjinger public C34 {35 public:36 D (int arecy int brecint c int d): B (arecy b), C (arecore c), A (a) {} / / parameters 37 ~ D () must be passed to the virtual base class here; 38}
When deriving, adding the keyword virtual to the corresponding inheritance mode prevents two class A members from appearing at the same time in Class D.
When we instantiate D, we pass a 1 to a, so what is the value of t in the virtual base class? Notice that we did the "+" operation on lines 14 and 25 when we passed values to the constructor of the virtual base class. )
The answer must be 1, because when instantiating D, the constructor of the virtual base class is called only once, so that the parameters received by the virtual base class can only be passed from the instantiation D.
Int mian () {D temp; return 0;}
Note: when instantiating class D, the C++ compiling system will only call the constructor of the virtual base class once, ignoring the calls of other derived classes of the virtual base class (class Bdepartment class C) to the virtual inherited constructor, thus ensuring that the data members of the virtual base class will not be initialized many times.
Thank you for your reading, the above is the content of "C++ virtual inheritance example analysis". After the study of this article, I believe you have a deeper understanding of the problem of C++ virtual inheritance example analysis. The specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.