Dynamic association learning: 1. polymorphism and dynamic association learning

Source: Internet
Author: User
Tags types of functions
Dynamic association learning: 1. polymorphism and dynamic association learning

Joint editing isCombine modules or functions to generate executable FunctionsCodeAnd allocate the memory address to each module or function call, and assign the correct memory address to external access.By associationDifferent stages can be divided into two differentAssociation Method:Static Association andDynamic Association. In the compilation phase, function implementation and function calling are associated as static concatenation,Static joinIn the compilation phase, you must understand the information that all functions or modules need to be checked for execution. The function selection is based on the type of pointer to an object (or reference. OtherwiseProgramThis association is called Dynamic Association during execution,Dynamic concatenationThe selection of member functions is not based on pointers or references, but on object types. Different object types will produce different compilation results.CAllBothStatic Association.C ++In general, the Association is also a static Association, but dynamic association is required once polymorphism and virtual functions are involved.

Polymorphism is the core of object orientation. Its main idea is that it can adopt multiple forms of capabilities and implement different implementations through a single username or user interface. Polymorphism is generally described"One interface and multiple implementations. InC ++SpecificallyUse base class pointerAccess the functions and methods of the derived class.

Let's take a lookStatic associated editingFor example, this static Association causes unexpected results.

 

View plaincopy to clipboardprint?

  1. # Include <iostream. h>
  2. ClassShape {
  3. Public:
  4. VoidDraw () {cout <"I am shape"<Endl ;}
  5. VoidFun () {draw ();}
  6. };
  7. ClassCircle:PublicShape {
  8. Public:
  9. VoidDraw () {cout <"I am circle"<Endl ;}
  10. };
  11. Main (){
  12. ClassCircle oneshape;
  13. Oneshape. Fun ();
  14. }

# Include <iostream. h> <br/> class shape {<br/> Public: <br/> void draw () {cout <"I am shape" <Endl ;} <br/> void fun () {draw () ;}< br/>}; <br/> class circle: Public shape {<br/> public: <br/> void draw () {cout <"I am circle" <Endl ;}< br/>}; <br/> main () {<br/> class circle oneshape; <br/> oneshape. fun (); <br/>}

 

the output result of the program is "I am circle" , however, the result of "I am shape" is actually output, the reason for this result is static Association. Static joint compilation requires to determine the function implementation during compilation, but in fact, the compiler cannot obtain the correct call function only when it only knows the address of shape , it only knows shape :: draw () , the final result can only be draw operation bound to shape class. The results of "I am shape" are not surprising.
to cause Dynamic Association, you only need to declare the dynamic join function as a virtual function. Dynamic linear editing only Functions for virtual functions. When we use the base class and only use the base class to access the derived class, as long as the base class contains virtual functions directly or indirectly (inherited from the upper layer, dynamic association is automatically awakened. Next we will slightly change the above program.

View plaincopy to clipboardprint?

  1. # Include <iostream. h>
  2. ClassShape {
  3. Public:
  4. Virtual VoidDraw () {cout <"I am shape"<Endl ;}
  5. VoidFun () {draw ();}
  6. };
  7. ClassCircle:PublicShape {
  8. Public:
  9. VoidDraw () {cout <"I am circle"<Endl ;}
  10. };
  11. Main (){
  12. ClassCircle oneshape;
  13. Fun (& oneshape );
  14. }

# Include <iostream. h> <br/> class shape {<br/> Public: <br/> virtual void draw () {cout <"I am shape" <Endl ;} <br/> void fun () {draw () ;}< br/>}; <br/> class circle: Public shape {<br/> public: <br/> void draw () {cout <"I am circle" <Endl ;}< br/>}; <br/> main () {<br/> class circle oneshape; <br/> fun (& oneshape); <br/>}

 

The program execution result is correct."I am circle". Code inVc6.0.

so far, we do not know the Dynamic Association execution mechanism, but we can make a guess. As mentioned above, the actual object types of functions should be different, and the association results should also be different. In static Association , the difficulty in execution is that you cannot use the base class to know the exact type of the sub-object to be joined. In 1.cpp shape derived class of span> is circle, or other rectangle or Square , which static joint editing should be used. The confusion is here. the exact object type of unknown concatenation must be used for dynamic concatenation during compilation., (If you know it, it becomes static Association ). Therefore, it can only use a certain mechanism, this allows you to find and call the correct function body during execution. As you can imagine, for this purpose, some relevant information should be encapsulated in the object itself. This information is somewhat like identity authentication, identify yourself, in this way, when dynamically orchestrating , the compiler can find the corresponding function body based on these tags, " do not run, " .

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.