C + + Scattered knowledge point shorthand--< in-depth exploration of C + + object model > Reading notes

Source: Internet
Author: User

The compiler always generates a default constructor when there is no constructor for error cognition

The compiler generates a default constructor only when necessary
The same as the destructor

Conditions:

    • There is a member with a default constructor
    • There is a base class with a default constructor
    • There's virtual function.
    • There's virtual inherit.
Any object with vptr/dynamic_cast operation on any object pointer

Only when an object has a polymorphic property does it have a vptr to dynamic_cast its pointer.

dynamic_cast A pointer without a polymorphic attribute causes the compiler to make an error.

    classGrandpa { Public:intACharA1;//virtual void Func () {}};classFather: PublicGrandpa { Public:intbCharA2; };classSon: PublicFather { Public:intICharA3; };intMain () {Son s;        Father F;        Grandpa * P1 = &s; Grandpa * P2 = &f;printf("The addr of P1:%p\n",dynamic_cast<Son*> (p1));printf("The addr of P1:%p\n",dynamic_cast<Father*> (p1));printf("The addr of P2:%p\n",dynamic_cast<Son*> (p2));return 0; }

Error:

c++_test.cpp:in function ' int main () ':
C++_test.cpp:42:63:error:cannot dynamic_cast ' P1 ' (of type ' class grandpa* ') to type ' class son* ' (source type was not PO lymorphic)
C++_test.cpp:43:66:error:cannot dynamic_cast ' P1 ' (of type ' class grandpa* ') to type ' class father* ' (source type was not polymorphic)
C++_test.cpp:44:63:error:cannot dynamic_cast ' P2 ' (of type ' class grandpa* ') to type ' class son* ' (source type was not PO lymorphic)

Cancels the mask of the virtual function of the grandpa, as a result:

The addr of P1:0x7fff8f017ee0
The addr of P1:0x7fff8f017ee0
The addr of P2: (nil)

The new knowledge point C + + language guarantees that "the base class object appearing in a derived class has its integrity" pointer to data Member meaning: Member example of an offset in an object:
    classGrandpa { Public:intACharA1; };classFather: PublicGrandpa { Public:CharA2; };classSon: PublicFather { Public:CharA3; };#include <iostream>    #include <stdio.h>    using STD::cout;using STD:: Endl;intMain () {cout<<"Sizeof Grandpa:"<<sizeof(Grandpa) <<endl;cout<<"Sizeof Father:"<<sizeof(Father) <<endl;cout<<"Sizeof Son:"<<sizeof(Son) <<endl;printf("The offset of A1:%p\n", &AMP;SON::A1);printf("The offset of A2:%p\n", &AMP;SON::A2);printf("The offset of A3:%p\n", &AMP;SON::A3);return 0; }
Output Result:

linux/g++

Command

g++ Test.cpp

Output

Sizeof Grandpa:8
Sizeof Father:12
Sizeof Son:12
The offset of a1:0x4
The offset of a2:0x8
The offset of a3:0x9

Window/visual Studio

Sizeof Grandpa:8
Sizeof Father:12
Sizeof son:16
The offset of a1:0x4
The offset of a2:0x8
The offset of a3:0xc

Analysis:
    1. Because of the address alignment, the size of theGranpa is 8byte instead of 5byte
    2. The offset of the A1 is 4

    3. The size of the Granpa class is 8byte , and the member of the derived class Father only starts at the 8th address (nineth bit).

    4. The offset of the A2 is 8, which is clearly implemented to maintain the integrity of the Grandpa class.
    5. For vs son the size of 16byte and A3 offset 12 is obviously also to maintain the Father class as is intact.
Problem:

Why is the size of son under LINUX/GCC 12? Why the A3 offset is 9 instead of 12

Change the code to:

    • An int was added to the Son class, declaring that it precedes A3 .
    classGrandpa { Public:intACharA1; };classFather: PublicGrandpa { Public://int B;            CharA2; };classSon: PublicFather { Public:intICharA3; };#include <iostream>    #include <stdio.h>    using STD::cout;using STD:: Endl;intMain () {cout<<"Size of Grandpa:"<<sizeof(Grandpa) <<endl;cout<<"Size of Father:"<<sizeof(Father) <<endl;cout<<"Size of Son:"<<sizeof(Son) <<endl;printf("The offset of a:%p\n", &son::a);printf("The offset of A1:%p\n", &AMP;SON::A1);printf("The offset of A2:%p\n", &AMP;SON::A2);printf("The offset of A3:%p\n", &AMP;SON::A3);printf("The offset of I:%p\n", &son::i);return 0; }

Output:

Size of Grandpa:8
Size of Father:12
Size of Son:20
The offset of a: (nil)
The offset of a1:0x4
The offset of a2:0x8
The offset of a3:0x10
The offset of i:0xc

You can see that the g++ has been properly optimized for memory, and the A3 is stored before I.

    • To add an int in the Father class
      To open in the above code //int b , get the output:

Size of Grandpa:8
Size of Father:16
Size of Son:24
The offset of a: (nil)
The offset of a1:0x4
The offset of a2:0xc
The offset of a3:0x14
The offset of i:0x10

This time there was no previous optimization. Can infer:

g++ if the parent class is also a derived class when the parent class is guaranteed to be intact, the parent class will only guarantee the intact integrity of the fragment .
That is, in the first code example, the intact integrity of the Father class in the son class is done by guaranteeing the Grandpa class's intact rows and the integrity of the data unique to the Father class char a2 . So though the size of the Father class is a byte, But the original integrity requires only 9byte!!!

C + + Scattered knowledge point shorthand--< in-depth exploration of C + + object model > Reading notes

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.