Discuss the "dynamic" and "static" of C ++ Polymorphism"

Source: Internet
Author: User

When we discuss polymorphism, Let's first look at what hard encoding and soft encoding are:
Hard coding means writing the code to death, resulting in insufficient flexibility and lower scalability. For example

If... else...
Switch... case...


These codes are usually hard-coded. If there are more codes in the project, it is equivalent
Flexibility, scalability, elasticity, and so on.

Therefore, we should try to use soft encoding. The popular point is "Don't let it go, leave some room for turning"
Polymorphism is a reflection of this soft encoding feature. Let's take a look at the polymorphism.

Polymorphism is an abstraction that abstracts the characteristics of things, and then we don't care about the specific form of things.

For example, for a job such as a worker, his characteristic is work. As for a worker, what does he do,
We don't need to worry about it, as long as we call it in the form of "workers. Work. Then he will work for us.

So why do we not abstract other features, but work features?
Because we are only interested in this feature, we don't care about its features such as eating, sleeping, and toilet.
With polymorphism, we can implement soft encoding!

After explaining the concept of polymorphism, let's look at the implementation of polymorphism (C ++ ):

The implementation of polymorphism is through the virtual function table (vtable). If each class has a virtual function, it has a virtual function.
Table. All objects share this vtable. This concept is also called Dynamic Association and static Association.
Some concepts are determined by the nature shown during program execution. Let's take a look at its "dynamic"
And static.

First look at a piece of code:

Class C0
...{
Public:
Void test ()
...{
Cout <"Call C0 test (). "<Endl;
}
};

This class does not have virtual functions. It is called statically. The call code is as follows:

// Static compilation (early bind early binding)
C0 * po0;
C0 obj0;
Po0 = & obj0;
Po0-> test ();

Its disassembly code is as follows:

// Call the function directly (the address is already known)
00401432 mov ECx, dword ptr [ebp-0Ch]
00401435 call @ ILT + 160 (C0: Test) (004010a5)

Let's take a look at the classes with virtual functions:

Class C1
...{
Public:
Virtual void test ()
...{
Cout <"Call C1 test ()" <Endl;
}
};

Class C11: Public C1
...{
Public:
Void test ()
...{
Cout <"Call C11 test ()" <Endl;
}
};

Its call:

C11 obj11;

C1 * pobj1;
Pobj1 = & obj11;
// The assembly code generated here
// 0040144a Lea edX, [ebp-14h] // addressing found pobj1
// 0040144d mov dword ptr [ebp-1Ch], EDX

Pobj1-> test ();
// The assembly code generated here
// 00401450 mov eax, dword ptr [ebp-1Ch] // get virtual table address
// 00401453 mov edX, dword ptr [eax]
// 00401455 mov ESI, ESP
// 00401457 mov ECx, dword ptr [ebp-1Ch] // obtain the test () function based on the location of the virtual table
// 0040145a call dword ptr [edX] // call the test () function

Based on the assembly code above, we can know that when calling a function with polymorphism, the program performs the following steps:
1. Locate pobj1 for addressing
2. Because C11 reloads the test virtual function, * pobj1 points to the C11 vtable address.
3. When pobj1-> test () is called, the program finds the vtable through vptr (virtual table pointer, first address of the object), and then calls the test function based on the offset.

Because the above multi-state call process is a dynamic process (you can find the function to call at runtime), instead of directly putting the function address there after compilation, therefore, it is called "Dynamic Association editing ".

The characteristics of polymorphism "motion" and "static" are described in combination with the code above, hoping to be clear.

Next, let's verify the problem of a class virtual table. If you are familiar with the virtual table, you don't have to look down.

The object model of C ++ has been illustrated in many books. Here we just do a verification. Take a look at this Code:

Class C1
...{
Public:
Virtual void test ()
...{
Cout <"Call C1 test ()" <Endl;
}
};

Class C11: Public C1
...{
Public:
Void test ()
...{
Cout <"Call C11 test ()" <Endl;
}
};

Class C12: Public C1
...{
Public:
Void test ()
...{
Cout <"Call C12 test ()" <Endl;
}
};

We can know that test () is a virtual function, and the class derived from C1 must have its own virtual table. Based on other materials, the virtual table pointer is placed at the first address of the object. Let's verify it as follows:

// Verify the first address
C11 obj110;
C11 obj111;

Printf ("obj110 address: % x", & obj110 );
Printf ("obj111 address: % x", & obj111 );
Printf ("obj110 virtual table address: % x", * (& obj110 ));
Printf ("obj111 virtual table address: % x", * (& obj111 ));

The result is:

Address of obj110: 12ff7c
Address of obj111: 12ff78
Address of the obj110 virtual table: 432098
Address of the obj111 virtual table: 432098

We can verify the above results:
1. A class is a vtable, not an object and a vtable.
2. The content of the object's first address is the vtable address.

Summary:
The polymorphism of C ++ includes its concept and implementation. This article discusses the C ++ polymorphism feature from the code generated by the compiler, and particularly explains why the polymorphism feature is called "Dynamic Association ", what is the difference between static Association and static association? What is the difference between them. In addition, the virtual table of the object is verified. Well, I hope this article will help you understand the C ++ polymorphism! Thank you!

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.