C + + virtual inheritance

Source: Internet
Author: User

Problem Description

When looking at Irrlicht source code, notice the following inheritance method:

Class Irrlichtdevice:public Virtual irefencecounted

{

...

};

Puzzled, then turn over the book to find answers, really is the book did not seriously look ah, at the same time read the code is a good way to learn.

Virtual Inheritance Description

In the "C + + Primer" Chinese version of the fifth edition of the No. 717 page of the author gave an example of "1" very good explanation of the situation, here I have complied.

The iostream and ostream of the IO standard library each inherit a common abstract base class named Base_ios. The abstract base class is responsible for saving the buffered content of the stream and managing the condition state of the flow. Iostream is another class that inherits directly from IStream and Ostream and can read and write the contents of the stream at the same time. Because IStream and Ostream both inherit from Base_ios, so iostream inherited Base_ios two times, once through IStream, another through ostream.

This leads to a problem where a iostream object is sure to want to operate in the same buffer. So if there are two copies, then the above share will not be realized.

In C + +, the purpose of virtual inheritance is to make a class declare a promise to share its base class. Where the shared base class sub-object is called the virtual base class. Thus, no matter how many times the virtual base class appears in the inheritance system, only one shared virtual base class sub-object is included in the derived class.

Simply put, in order to solve B, c simultaneously inherit a, and D inherits B, C, D has two copies of a in the question "2".

Reference Index

"1" "C + + Primer Chinese Version": Fifth edition, (United States) Lippmann (lippman,s.b), (United States) Lajoie (Lajoie,j.), (United States) (MOO,B.E.)

"2" http://bbs.chinaunix.net/thread-1782309-1-1.html

C + + virtual inheritance

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.