Underlying Mechanism and mechanism of c ++ classes

Source: Internet
Author: User

Underlying Mechanism and mechanism of c ++ classes
First, let's clarify the underlying mechanism of the class from A question: if there is A class A, there is A member function get (), for example: class A {public: void get ();} A a; then. what does get () mean? The first answer is get (& a), because in the underlying mechanism of the class, the first parameter of the member function is a pointer to the data structure of this class (except for static member functions ), therefore, the get () function of the member function is in the form of void get (A * this). this also shows why we can always use this in the definition of the member function to refer to the called object. We know that to use a C ++ class, the necessary condition is that the header file of this class can be obtained during the compilation period, you can also find the link addresses of the corresponding symbols (such as member functions and static data members) during the link period ). If the C ++ class is in the same project as your user, this condition is well met: first, the header file of the C ++ class is well obtained. The user can include the Class header file directly. Secondly, the C ++ class is often used by the compiler as a compilation unit to generate an obj file. During the final link process, the linker will link all obj links in the project to generate the final binary target file. Therefore, when the linker encounters a class member function (or other forms of symbolic reference), it will find the symbolic link address in the obj file generated by this class. So what is the need to use a C ++ class in the Code during the compilation and link phases? In other words, the compiler and the linker will not complain about what conditions are met? According to the definition of C ++, a C ++ class declares or defines the following types of content: 1. declares a data structure, non-static data members in the class, virtual table entry address pointers that cannot be seen in the Code but will be generated if there is a virtual function. 2. Declare and define a bunch of functions. Their first parameters are a pointer to this data structure. These are actually non-static member functions (including virtual functions) in the class. Although they are written inside a pair of braces in the class declaration, however, nothing is actually added to the internal data structure described in the preceding 1st. In fact, such a declaration only adds two attributes to these functions: the scope of the function name identifier is limited to the class; the first parameter of the function is this, which is ignored. 3. Declare and define another bunch of functions. They seem to be some common functions and have almost no relationship with this class. These are actually static functions in the class. They are the same. They do not add anything to the internal data structure described in Article 1st, but the scope of the function name identifier is limited to the class. 4. Declare and define a bunch of global variables. These are actually static data members in the class. 5. Declare and define a global variable, which is a function pointer array used to save the entry addresses of all such virtual functions. Of course, the premise for generating this global variable is that this class has virtual functions. Take the following example: class MyClass {
Public:
Int x;
Int y;
Void Foo ();
Void Bar (int newX, int newY );
Virtual void VFoo ();
Virtual void VBar (int newX, int newY) = 0;
Static void SFoo ();
Static void SBar (int newX, int newY );
Static int sx;
Static int sy ;}; for the class MyClass listed above, most C ++ compilers will compile in the following way: now let's take a look at why the compiler needs header files and symbolic addresses to compile a program that uses MyClass. First, the compiler needs to know the memory layout of the class during the compilation period to ensure that the correct code for opening up the memory and the sizeof (MyClass) values can be generated. With the header file, the compiler will know that a MyClass occupies 12 bytes of memory (see two integers and a pointer ). Secondly, when calling member and static functions of MyClass, the linker needs to know the entry address of these functions. If the entry address cannot be provided, the linker reports an error. Finally, when referencing static data members of MyClass, the linker needs to know the addresses of these variables just like referencing an external global object. If the address of these variables cannot be provided, the linker also reports an error. It can be seen that: 1. compilation period: the Class header file must be provided so that the compiler can know the size and memory layout of the class instance. 2. Link period: You must provide the addresses of member functions, static functions, and static data members referenced in the program so that the linker can correctly generate the final program. Here, we can guess that, in fact, to export a class, the compiler actually only needs: member functions, static functions, and static data members can be exported as common functions and global variables. In other words, we do not actually "export a class", but export the endpoint address of the "defined entity" to be referenced in this class as normal functions and variables. Because the pure virtual function VBar is not defined in it, it will not be exported.

What is the underlying mechanism of C language?

What is it? C language calls system functions, or library functions. It implements your programming function.
In addition, through the TC, VC, GCC and other compilers to compile the C language into an exe file, it is also the first compilation, I feel that all need to compile that step. For the syntax analysis of lexical analysis, refer to the compilation principle.

In linux, what does C language need to learn from underlying software development?

Directly recommend a few books
APUE is a book developed by C in linux. It provides detailed explanations for system calls and underlying linux mechanisms.
Www.amazon.cn/..est-23
There is also C expert programming. Many people use this book to fully understand the C language.
Www.amazon.cn/..est-23

Bottom-layer development has different branches. To put it bluntly, you are familiar with the C language. There is no difference between bottom-Layer Development and you only need to understand the mechanism. I suggest you read the book "Programmer self-cultivation" first.
Www.amazon.cn/..est-23
This book provides a more general and detailed explanation of some C language and system structure mechanisms.

Many books are recommended for kernels and drivers, such as the typical ldd and kernel scenario analysis. Links to these books are as follows:
Www.amazon.cn/..est-23
Www.amazon.cn/..est-23
Www.amazon.cn/..est-23

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.