The memory layout for C ++ object

Source: Internet
Author: User
Record your learning notes. ATL under the hood-Part 1

Http://www.codeproject.com/KB/atl/atl_underthehood_.aspx

Tells the class memory model of the object, including the memory model between classes with inheritance relationships

View code

 1   # Include  <  Iostream  >  
2   Using Namespace STD;
3
4   Class Base {
5   Public :
6 Base (){}
7 Virtual Void F1 () = 0 ;
8 Virtual Void F2 () = 0 ;
9 Int Bi;
10 };
11
12   Class Base2 {
13   Public :
14 Base2 (){}
15 Virtual Void F1 () = 0 ;
16 Virtual Void F2 () = 0 ;
17 Int Bi2;
18 };
19   Class Drive: Public Base {
20   Public :
21 Drive (){}
22
23 Int Di;
24 };
25
26   Class Mostdrive: Public Drive, Public Base2 {
27   Public :
28 Mostdrive (){}
29 Virtual Void F1 () {cout < " Mostdrive: F1 " < Endl ;}
30 Virtual Void F2 () {cout < " Mostdrive: F2 " < Endl ;}
31 Int DDI;
32 };
33
34   Int Main ()
35 {
36 Mostdrive D;
37
38 Return 0 ;
39 }

Object Model (in vc2005)

ATL under the hood-Part 2

Http://www.codeproject.com/KB/atl/atl_underthehood_2.aspx

Q: constructor cannot call virtual functions. Why?

1. Virtual tables will be initialized in the constructor.

2. In the inheritance chain, constructor calls are initiated from the top layer of the inheritance chain, and the last one calls the constructor of the object itself.

3. At each layer of the constructor, it will overwrite the initialized virtual table of the base class.

4. In each constructor, the compiler calls the userCodeComplete the preceding three steps.

Therefore, the virtual table of each constructor layer contains the self-re-written function. If you call the virtual function to find the virtual table, you can find the self-defined function.

Problem: the principle of pure virtual functions in C ++.

Semantics: indicates the class. It cannot be instantiated and can only be used as a base class or an excuse. For example, for some basic classes, shape instantiation is meaningless, so it is considered as an abstract class and implemented using pure virtual functions.

Especially in COM, C ++ does not provide the interface keyword. Pure virtual functions are used to simulate the interface keyword.

Implementation: For convenience of debugging, C ++ customizes a function. If the function is a pure virtual function and is not implemented, VC provides a function to replace the address of the function. If the virtual function is called, an error is returned.

Can the destructor of an abstract class be pure virtual functions?

Semantics: Of course.

Implementation: However, the destructor of the derived class calls the virtual destructor of the base class. If the implementation body of the base class's destructor cannot be found during link, a compilation error is reported.

Compromise: Provides the implementation body for pure destructor of the base class.

Problem:# DefineAtl_no_vtable_ Declspec(Novtable)

_ Declspec (novtable) is Microsoft
C ++ specific extended attribute of class.

When it is used then Compiler
Won't generate the code to initialize the vptr and vtable and reduce
The generated code size.

Because in the inheritance chain, the class has pure function shocould not be instanced, but it can initialize the virtual table.

You can image that if the inheritance chain is much longer, it will it much time and space to initilze the object from the last node in the inheritance chain.

Problem: virutal inheritance

Requirement:

When Diamond inheritance exists, the number of base classes is controlled.

Several moreArticle:

1. ATL under the hood Part 3 describes function templates, class templates, typename, and special features, and simulates polymorphism using templates.

2.ATL under the hood Part 4

3. ATL under the hood Part 5 Introduction to thunk in ATL


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.