Memory Distribution and virtual function table of C ++ objects

Source: Internet
Author: User

C ++ has four types of members: static data members and non-static data members, static functions and non-static functions.

1.Non-static data memberPutEvery objectThe body acts as a proprietary data member of the object.
2.Static Data MemberExtracted and placed in the static data area of the program.Share all objectsTherefore, only one copy exists.
3.Static and non-static member functionsFinally, they are extracted and placed in the code segment of the program.All objects in this class are shared.Therefore, each member function can only have one code entity. In C ++, all member functions of the class are stored in the static storage area, and static functions are also stored in the static storage area. They all save the same copy in the class.
Therefore,Only data that constitutes the object itself, No member function is affiliated with any object,The relationship between non-static member functions and objects is binding, and the bound intermediary is the this pointer.. Member functions share all objects of this class. They are not only for simplified language implementation and storage saving, but also for consistent behavior of similar objects. Although the behavior of similar objects is consistent, different data members are operated.

# Include "iostream. H"

Class
{
PRIVATE:
Int X;
Int y;
Public:
Void sety ()
{
Y = 5;
}
Void print ()
{
Cout <"x =" <x <Endl <"Y =" <Y <Endl;
}
};

Void main ()
{
A;
A. sety ();
A. Print ();
Int * P = (int *) & A; // it is reasonable to have only non-static data members in the memory model of object.
* P = 6;
A. Print ();
}

4.Memory layout of a single inherited object, The first isVirtualFunctionTableThe pointer vtbl, which is a member and then a base class and a subclass. The virtual function table contains the addresses of all virtual functions and ends with null.If the subclass of a virtual function is overwritten, it is replaced by the subclass..

<1>Explanation of the above class diagram: V2 of B covers V2 of A, V1 of C covers V1 of A, and V3 of C covers V3.

Note:The covered features include:

1) different ranges (located separatelyDerived class and base class);

2)Same Function Name;

3)Same Parameters;

4) basic functions are required.With Virtual keywords.

Member function isOverloaded features
(1) the same range (inIn the same class);
(2) The function name is the same;
(3) parameters are different;
(4)VirtualKeywordsOptional.
"Hide"YesThe function of the derived class shields the base class functions with the same name.The rules are as follows:
(1) If the function of the derived class andFunction name, but different parameters. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
(2) If the function of the derived class andThe function has the same name and parameters, but the base-class function does not have the virtual keyword.. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)

<2>The memory model of class C objects is:


Layout of a single inherited object, the first is virtualFunctionTable pointer vtbl, which is later a member and then a base class and a subclass. The virtual function table contains the addresses of all virtual functions and ends with null. If the subclass of a virtual function is overwritten, it is replaced by the subclass.
5. What is the same as single inheritance is that all virtual functions are included in the virtual function table.Duplicate inheritance has multiple virtual function tables, WhenWhen a subclass overrides the virtual function of the parent class, Subclass functionReplaceThe function of the parent classVirtual Function Location, WhenWhen the subclass has a new virtual function, These virtual functionsAdded after the first virtual function table.

<1>Interpretation of Class Diagrams: V3 of D covers V3 of B3, and D has a new virtual function VD.

<2>Classd Memory Model


Summary: What is the same as single inheritance is all virtualFunctionAll are included in the virtual function table. Different multi-inheritance types have multiple virtual function tables. When the subclass overrides the virtual function of the parent class, the subclass function overwrites the parent class function in the corresponding virtual function location. When the subclass has a new virtual function, these virtual functions are added after the first virtual function table.

6. Virtual inheritance makes the public base class have only one copy in the subclass. We can see that the vbtable is added to the multi-inheritance to store the offset to the public base class.

<1>Class Diagram Explanation: D2 covers VB of B, while GD covers vd1 of D1 and VB of B. Gd also has its own virtual function VGD.

<2>Memory Model of class GD


Summary: Virtual inheritance makes the public base class have only one copy in the subclass. We can see that the vbtable is added to the multi-inheritance to store the offset to the public base class.

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.