Reprinted from: http://blog.csdn.net/blucexi/article/details/1253265
Joint editingThat is, the process of generating executable code by combining modules and functions, allocating memory addresses for each module or function call, and allocating the correct memory addresses for external access, it is a process in which computer programs are associated with each other. There are two different association methods according to different stages of association Compilation:Static AssociationAndDynamic Association.
Static AssociationIt means that function implementation and function calling are associated at the compilation stage. Therefore, static concatenation is also called early binding, in 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). in C language, all associations are static associations. As far as I know, any compiler supports static Associations (nonsense ).
Dynamic AssociationIt means that function implementation is associated with function calling only when the program is executed. Therefore, it is also called runtime binding or late binding. Dynamic concatenation does not select a function based on pointers or references, it is based on the object type. Different object types make different compilation results. In C ++, linear editing is usually static, but dynamic association is required once polymorphism and virtual functions are involved. The following describes polymorphism.
PolymorphismThe literal meaning is that it has multiple forms or forms. There are two types of C ++ polymorphism: Dynamic polymorphism and static polymorphism. Dynamic polymorphism refers to general polymorphism, which is realized through class inheritance and virtual function mechanism; static polymorphism is implemented through templates, because such polymorphism is called static polymorphism when it is compiled instead of runtime.
Examples of dynamic polymorphism:
# Include <stdio. h> # include <iostream>/*** shape */class cshape {public: cshape () {} virtual ~ Cshape () {} virtual void draw () = 0 ;};/*** point */class cpoint: Public cshape {public: cpoint (){}~ Cpoint () {} void draw () {printf ("Hello! I am point! /N ") ;}};/*** line */class Cline: Public cshape {public: Cline (){}~ Cline () {} void draw () {printf ("Hello! I am line! /N ") ;}}; void main () {cshape * shape = new cpoint (); // draw pointshape-> draw (); // here, shape will call the draw () function of cpoint to delete shape; shape = new Cline (); // draw lineshape-> draw (); // here, shape will call the draw () function of cline to delete shape; return ;}
You can think about the output result. If you don't know it, run it!
From the above example, we should be able to understand what is polymorphism: that is, a draw () can have two implementations, which are determined at runtime and unknown during the compilation stage, it's impossible to know! Only when running can we know that the shape we generate is that kind of image. Of course, to achieve this effect, we need to dynamically compile it, in the base class, we declare the function to be polymorphism as a virtual function, and the implementation principle of the virtual function uses dynamic concatenation. If you want to know more thoroughly, check the information on the Internet! Haha! Here I will not explain the implementation principle of the virtual function.
Here, we also provide an example of static polymorphism:
Add a template function based on the above example:
Template <class T> void drawshape (T * t) {T-> draw ();} modify the main function as follows: void main () {cshape * shape = new cpoint (); // draw pointshape-> draw (); drawshape <cpoint> (cpoint *) shape); Delete shape; shape = new Cline (); // draw lineshape-> draw (); drawshape <Cline> (Cline *) shape); Delete shape; return ;}
When the program compiles the main function, the compiler has already specified the draw in the drawshape function to call that implementation, which is static polymorphism, the function to be called is known during compilation.