Dynamic Association and static Association

Source: Internet
Author: User

With regard to the concept of Dynamic Association and static Association, I still do not understand the principle of the course I attended. In this case, you can only learn by yourself. First, we know that dynamic and static associations are a manifestation of polymorphism. There are three basic elements of object-oriented: encapsulation (type abstraction), inheritance and polymorphism. First, we understand the functions of dynamic and static association from the conceptual perspective: Implementing polymorphism. Then let's start with the most basic explanation. 1. What is composite? Association refers to the process in which a computer program is associated with each other. During the association process, you must determine the operation calls (function calls) in the program and execute the operation (function) the ing relationship between code segments. according to the different stages of the association, it can be divided into static Association and Dynamic Association. Read the sentences in red carefully. We can clearly understand what association is. Let's give you an easy-to-understand example. Class A has the fun function, and Class B also has the fun function. Now I call the fun function in the main function outside the class. The main function is A function call, and the fun function is called. The fun function in Class A and the fun function in Class B are the code segments that execute this operation. Therefore, the linking function is to implement the ing between the two. [Cpp] class A {void func () {cout <"It's A" <endl ;}; class B {void func () {cout <"It's B" <endl ;}; int main () {func () the function call is mapped to the func function in A or the func function in B. 2. the definition of static Association and Dynamic Association makes it difficult to understand dynamic Association and static Association: it means that the compilation work is carried out in the compilation and connection phase of the program. This kind of association is also called the Early Association, because such association is completed before the program starts to run; this kind of association during the program compilation stage is also called static bundle; during compilation, the relationship between the operation calls in the program and the code that executes the operation is solved, determining this relationship is also called bundle; Compiling is also called static bundle; Taking the above example, static concatenation determines whether the main function calls func in a or func in B during compilation. Once the compilation is complete, their ing relationships are uniquely identified. Dynamic concatenation: during the compilation phase, the Compilation Program does not know exactly the function to be called. Only when the program is executed can the function to be called be determined, to this end, You Need To Know exactly the function to be called and require that the compilation work be performed during the running of the program. This kind of compilation work is called dynamic association or dynamic bundle during the running of the program, C ++ stipulates that dynamic Association is implemented with the support of virtual functions; during compilation, I still don't know which func function should be selected. It is determined only when the function is actually executed. Static Association and Dynamic Association are all polymorphism. They choose different implementations at different stages. In fact, the essence of polymorphism is choice. There are many choices, so there is polymorphism. 3. for static concatenation, let's take an example. [Cpp] # include <iostream> using namespace std; class shape {public: void draw () {cout <"I am shape" <endl;} void fun () {draw () ;}}; class circle: public shape {public: void draw () {cout <"I am circle" <endl ;}}; void main () {circle oneshape; oneshape. fun () ;}now we have defined in detail the Class A and Class B at the beginning and the func functions. Let's analyze: When oneshape. fun () is called, enter the fun function in the shape class. Now our question is: is the draw called by the fun function the draw in the shape or the draw in the circle ?? The answer is: it calls the draw function of the base class cshape. So I am shape has always plagued me: Why do I call the draw of the base class instead of the draw in the derived class? It seems that there is no specific explanation in the book, and the teacher won't talk about it at all during class. After thinking about it, you can understand it from the perspective of Assembly: 1. call oneshape. fun (), here is a jump command, enter the code segment where the fun function in the shape class is located 2. the code segments of the class shape are placed sequentially. After entering the fun function, we need to call the draw address. Because there is no other data structure to store the draw address, what the program knows must be the draw address in the shape class, the Code 013B1546 call shape: draw (13B10F5h) clearly indicates shape: draw in my vs2010 disassembly debugging window, the ing is also determined and the association is completed. 4. Dynamic Association we see the shortcomings of static Association in section 3rd (this is what we say in the general textbook ). As I mentioned earlier, because there is no other data structure to store the draw address, what the program knows is the draw address in the shape class, we only use a jump command www.2cto.com, so we want to implement dynamic compilation. In fact, we want to create a data structure, which is used to store ing relationships, that is, Association. Therefore, c ++ has implemented a virtual function. In fact, the essence of virtual functions is to build a data structure. That is to say, the virtual function table does not need to be pulled out. It is a huge string. Let's go to the following blog to learn it. Http://blog.csdn.net/haoel/article/details/1948051/ 4.1 virtual functions do not like to understand the concept. I do not like it either. But the concept is important after all. I have already explained why virtual functions should be introduced. So let's take a look at the following concepts: virtual functions are the basis for dynamic Association; virtual functions are member functions and non-static member functions; A virtual function may have different implementations in a derived class. When this member function is used to operate a pointer or reference the identified object, dynamic concatenation is used for calling the member function, namely: when the program is running, associate or bundle the call relationship. Dynamic Association can only operate virtual functions by pointer or referencing the identification object. If a general identification object is used to operate virtual functions, static concatenation is used to call virtual functions. If a class has virtual functions, the compiler will define a pointer member for the class objects, and let the pointer member point to a table where the entry address of the class virtual function is stored. For example, a base class contains some virtual functions, the base class has such a table, which stores the entry address of its own virtual function. Its derived class inherits the virtual function table, if the virtual functions in the base class are overwritten or modified in the derived class, the compiler modifies the function entry address in the virtual function table to the corresponding virtual function entry address in the derived class; this provides a basis for the implementation of class polymorphism. The virtual functions are stored in the virtual function table in the declared order. The virtual functions of the parent class are stored before the virtual functions of the subclass, each parent class has its own virtual function table. member functions of sub-classes are stored in the virtual function table of the first parent class. 4.2 examples of Dynamic Association are provided, in the example in section 3, the following code only adds the word virtual [cpp] # include <iostream> using namespace std; class shape {public: void virtual draw () {cout <"I am shape" <endl ;}// the draw is set to void fun () {draw ();}}; class circle: public shape {public: void draw () {cout <"I am circle" <endl ;}// although it does not indicate that draw in the circle class is a virtual function, however, circle actually follows Fulfill the virtual nature}; void main () {circle oneshape; oneshape. fun ();} Now let's run the program again, and the output will be the I am circle disassembly code posted here. If you have time, see [plain] 00CC15A0 push ebp 00CC15A1 mov ebp, esp 00CC15A3 sub esp, 0CCh 00CC15A9 push ebx 00CC15AA push esi 00CC15AB push edi 00CC15AC push ecx 00CC15AD lea edi, [ebp-0CCh] 00CC15B3 mov ecx, 33 h 00CC15B8 mov eax, 0 CCCCCCCCh 00CC15BD rep stos dword ptr es: [edi] 00CC15BF pop ecx 00CC15C0 mov dw Ord ptr [ebp-8], ecx 00CC15C3 mov eax, dword ptr [this] 00CC15C6 mov edx, dword ptr [eax] 00CC15C8 mov esi, esp 00CC15CA mov ecx, dword ptr [this] 00CC15CD mov eax, dword ptr [edx] 00CC15CF call eax 00CC15D1 cmp esi, esp 00CC15D3 call @ ILT + 440 (_ RTC_CheckEsp) (0CC11BDh) 00CC15D8 pop edi 00CC15D9 pop esi 00CC15DA pop ebx 00CC15DB add esp, 0CCh 00CC15E1 cmp ebp, esp 00CC15E3 call @ ILT + 440 (_ RTC_CheckEsp) (0C C11BDh) 00CC15E8 mov esp, ebp 00CC15EA pop ebp 00CC15EB ret, etc. After reading the virtual function table, let's take a good look.

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.