Introduction to C + + object Model (ii)--"Deep Exploration of C + + object Model" thin notes

Source: Internet
Author: User
Tags inheritance object model

three

Multiple Inheritance

Class A
{public
:
    A () {}
    Virtual ~a () {}
    virtual int. Foo ()  {return  val;  }
    virtual int Funa () {}
private:
    int val;
    char bit1;
} ;

Class B:
{public
:
    B () {}
    Virtual ~b () {}
    virtual int-foo ()  {return  bit2;< c25/>}
    Virtual int funb () {}
private:
    char bit2;

Class Derived:public A, public B
{public
:
    Derived () {}
    Virtual ~derived () {}
    virtual int foo ( )  {   return  bit3;  }
    virtual int funderived () {}
private:
    char bit3;


Note: Under multiple inheritance, if there are N base classes, there are n virtual table in the derived class.

For each virtual table, there is a corresponding vptr in the derived class object. These vptrs will be set in the constructor in the initial value.

a virtual function of a derived class overrides (overwrites) the corresponding virtual function index value in each of its base class virtualtable.

multiple inheritance the leftmost base class, in a derived class as the primary entity, whose virtualtable is the primary table , and the other base class's virtual table as the secondary table.

When you assign a derived object address to a Base1 pointer or derived pointer, the virtualtable being processed is the primary form VPTR_BASE1.

Therefore, the main table contains all the virtual functions of the derived (including inherited virtual functions). Therefore, there are virtual functions in Base1, Base2 and derived.

The number of items in other secondary tables is unchanged, but the index values of some virtual functions are overridden.

conversion of pointers involving multiple inheritance

The problem of multiple inheritance occurs primarily in the transition between a derived class object and its second or subsequent base class object.

"For a multiple-derived object, assign its address to a pointer to the" left (that is, first) "base class, which will be the same as a single inheritance because both point to the same starting address. The cost must be paid only for the specified operation of the address. For the second or subsequent base class address-specific operation, you need to modify the address by adding (or subtracting) the base class Subobject (s) size in the middle.

For example:

Derived Dobj;

A * PA = &dobj;

Just a simple copy of the address is all you need.

and

derived* PD;

b* PB = PD;

This internal transformation is required:

Virtual C + + code

PB = PD? (b*) ((char*) PD + sizeof (A)): 0;

 

Establishments

multiple inheritance with virtual inheritance

1. Virtual base class no data member

Cases:

Class A {  };
Class B:public virtual A {  };
Class C:public virtual A {  };
Class D:public B, public C {};

"Note" class A {}; it's not actually empty, it has a cryptic 1 byte, which is a char inserted into the compiler. This is so that class A objects can be configured with unique addresses in memory.

When a language supports a virtual base class, it can cause some additional burdens. There is an extra pointer in the derived class that points to a related table in which either the virtual base class object or the offset is stored in the table.

If the virtual base class is empty, a virtual base class object (that is, an insert byte) is stored in the table.

VC + + Compiler optimization:

VC + + special on the empty virtual BaseClass did the processing. The void base class has only a 4-byte pointer in its derived class. No char is placed, and no byte fills.

(Because the purpose of the placement of char is to have an address in memory for an object instantiated by an empty class, and now that it has a pointer in its derived class object, it can take an address, so no char is required)

Note: If there are data members in the virtual base class, the two compilers ("with special handlers" and "no special handlers") produce exactly the same object layout.

2, virtual base class has data members

Cases:

Class A 
{public
:
     ...
Private:
    int x, y;
};

Class B:public Virtual A 
{public
:
    ...
Private:
    int valb;
};

Class C:public Virtual A 
{public
:
    ...
Private:
    int valc;
};

Class D:public B, public C 
{public
:
    ...
Private:
    int vald;
};


The end of the derived class object is the part of the shared virtual base class, and the virtual function table that the pointer to the derived class B and class C points to has an additional entry: offset(the number of bytes from the beginning of the object to the part of the shared virtual base class)

This allows you to quickly access members of a shared virtual base class.

Ask:

① Why virtual inheritance is not like the general inheritance, put the base class members at the top, and put the new derived class members on the tail.

I think it is: because in the last multiple inheritance, the final derived class is required to have only one base class member in the object, the resulting derived class extracts their respective derived data members from each of their parent classes. It is difficult to extract the data because it is easy to find the derived class members in the parent class, but it is difficult to determine the boundary. In order to extract the data conveniently, the base class member is put on the tail.

② Why there are two virtual pointers in the derived class for virtual inheritance.

I would like to: because to achieve polymorphism. When a base class pointer points to a base class part in a derived class, you must have a pointer to the virtual table in order to achieve polymorphism.

Note: The Class D object model is similar to the layout of a derived class object that is typically multiple-inherited, and the virtual table of the leftmost base class portion of multiple inheritance is "primary table." So class B, class C, Class A part of the virtual table is different. They are all meant to achieve polymorphism.

"Programming Style" in general, one of the most effective forms of virtual base class is an abstract virtual base class with no data members.


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.