"Deep Exploration of C + + object Model" Learning notes-3 data semantics

Source: Internet
Author: User

1. Class object size is affected by three aspects: 1 The burden of virtual base or virtual Func, 2 is ebo;3) alignment (sizeof Daquan).

2. Virtual base class represents "only a single, shared entity", regardless of how many times it appears in the inheritance system.

3. C + + does not enforce the order of "base class Subobject Memory Order" and "data members in different access-level classes."

4. Static data members are placed in the global data segment and do not affect the size of an individual object.

5. Data member type bindings, member function pointers see the Code section. About the member function pointer, to understand the object's sizeof.

6. Access member variable m by X's Object x and pointer pt, when there will be any significant difference. When M derives from the virtual base class of X.

7. In memory, the base class appears in front of the subclass, but the virtual base class is at the end of the entire layout, and if there are more than one virtual base class, it is arranged in a declaration order.

8. Inherit the effect on the data members of the class (see the Appendix of the series for details):

A simple, polymorphic inheritance. Does not increase memory space and the additional burden on access. The C + + language guarantees that the base class child object (base class Subobject) that appears in the subclass is to maintain its integrity ", that is, to remove the base class child object at any time is a complete whole." Be aware of alignment issues.

b) plus polymorphism (with virtual function)

Ø Cost: 1 vtbl (including typeid); 2) Add vptr, retrofit 3) constructor and 4 destructor to process vptr. VC puts Vptr in the front of the object (if there is a virtual base class, Vptr will be after the offset pointer), even if the base class has no virtual function but only subclasses.

Ø the subclass shares the same vptr as the first parent, and a second base class increases the vptr. Adding new or modified virtual functions to a subclass updates the contents of the corresponding VTBL.

c) Multiple inheritance: The problem arises primarily in the conversion between a derived class object and its second or subsequent base class object, because it involves the offset of the pointer.

D virtual inheritance that class contains n a virtual base class

Ø represents that it can be cut into two parts: the invariant part (the front) and the shared part (put behind).

Ø the data in the invariant section, no matter how the successor changes, the offset is always fixed, while the shared part represents a child of the virtual base class, and its offset varies because of an increase in the derived hierarchy, whether at the bottom or the top, because it is always at the end of the entire class object.

Ø The offset pointer to the virtual base class is arranged in the derived class, and an offset is required to locate the virtual base class member.

9. With regard to the effect of alignment on sizeof, only: 1 All of the "base class child objects" are all char, or 2) virtual base is all char; in both cases, odd size (19,23, etc.) may occur, and other cases must be aligned to int, * (e.g. vptr) , double, and so on.

Some experimental code:

#ifndef ch3_h #define CH3_H namespace ch3{//Data semantic//Other parts of this chapter, see Additional works: SIZEOFTEST.DSW//p96 namespace, static_i nherit{//Parent class and subclass share the same XX object, one modified, the other is also modified class cls{public:static int xx; int cls::xx = 8; class Der:public cls {}; v OID Test () {CLS C1; c1.xx = ten der D1; d1.xx = 20;}} P90 namespace global_cls_var{//In the CLS, if there is a variable with the same name as global, then the variables in the class are given precedence//that is, the entire program will begin parsing the class's members//functions, and so on, the int myty Pe No1= class cls{Public:float get () {return mytype;}; public:float mytype;//No2 = No1, same name. No2 precedence over No1}; void Test () {MyType = 8; cls C; c.mytype = 2; int tmp = (int) c.get (); if (tmp = = MyType) {//Global priority printf ("Variable selection Global priority n"); else if (tmp = = C.mytype) {//Local priority printf ("Variable selection local priority n");} else {printf ("Impossible, Strange/n");} }//P91 namespace type_def{//a: For the type in the parameter list of the function of the class, or function return value type://1) in the CLS, the definition of typedef is after the use of MyType,//will compile the error;//2 If the Typ in the CLS Edef definition before the use of MyType,//program first select the typedef within the class//b: for class member variables, always use the global typedef. Also, the local typedef will not compile the error, either before or after it. Here, use it.Very troublesome, to be noted. I think the fundamental way to avoid this is: do not define the same thing,//save trouble for the typedef int MYTYPE; N0.1 class test{public:float F;}; The class cls{//local typedef definition is legal than using the pre-public://BEGIN--->///below definition. Description: From the global typedef--> local typedef--> MEM func, a process like this, the typedef inside the class is given precedence over global and the TypeDef test m is used Ytype N0.2 ===== N0.1, type not the same/note: The use of m->f means that this m is definitely a test type, not int type void Test (mytype* m) {m->f = 0;}; Function arguments after the definition//end <<----int n; MyType p; Variables are used after the definition}; Class cls2{//local typedef definition is more than using Public:int N; mytype p;//variable used before definition void test (mytype* m) {};//function parameter before definition///below definition if it appears, it will The error://error C2327: ' Cls2::test ': Member from enclosing class/are not. A type name, static, or enumerator/description: From Global typedef--> MEM func--> local typedef,//After such a process is down, the TypeDef within class will be considered illegal//typedef test mytype; N0.3 ===== N0.1, not the same type}; void Test () {//The following two outputs are the "global Works"//CLS length of int + MyType.//MyType = = Int (4). or MyType = = Test (8) int n = sizeof (CLS); if (n = sizeof (int) * 2) {//mytype = = Int (4) printf ("Cls:typedef global works/n");} else {//mytype = = float (8) printf ("Cls:typede F class Inner works/n "); The length of the//CLS2 is int + mytype. MyType = = Int (4). or MyType = = Test (8) n = sizeof (CLS2); if (n = = sizeof (int) * 2) {//mytype = = Int (4) printf ("Cls2:typedef global works/n");} else {//mytype = = float (8) Prin TF ("Cls2:typedef class inner works/n"); }}//main test void Test () {printf ("CH3 begin/n"); Static_inherit::test (); Global_cls_var::test (); Type_def::test (); p RINTF ("CH3 end/n"); } }; #endif   

 

Related Article

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.