Static and Dynamic Association of C ++

Source: Internet
Author: User

Reprinted: http://www.yesky.com/23/1621023.shtml

A computer program is associated with each other. According to the different stages of association editing, there are two different association methods: static Association editing and Dynamic Association editing.

  Static Association

Static Concatenation refers to the association that occurs in the compilation and connection phase. Such association is also called early association because the association process is completed before the program starts running.

Such association during compilation is also called static bundle. During compilation, the relationship between the operation calls in the program and the code executing the operation is solved. determining this relationship is also called bundle, and static bundle is also called bundle during compilation. The following is an example of static Association.

# Include

Class Point
{
Public:
Point (double I, Double J) {x = I; y = J ;}
Double area () const {return 0.0 ;}
PRIVATE:
Double X, Y;
};
Class rectangle: Public point
{
Public:
Rectangle (double I, Double J, Double K, double L );
Double area () const {return w * h ;}
PRIVATE:
Double W, h;
};
Rectangle: rectangle (double I, Double J, Double K, double L): Point (I, j)
{
W = K; H = L;
}
Void fun (point & S)
{
Cout <S. Area () <Endl;
}

Void main ()
{
Rectangle Rec (3.0, 5.2, 15.0, 25.0 );
Fun (REC );
}

The running result of this program is:

0

The output shows that in the fun () function, the area () operation executed by the object referenced by S is associated with the implementation code of point: Area. This is because of the results of static Association. During the program compilation stage, the area () operation on the object referenced by S can only be bound to the function of the Point class. Therefore, the program outputs unexpected results. We expect that the area () operation executed by the S-referenced object should be bound to the area () function of the rectangl class. This is not achieved by static Association.

  Dynamic Association

From the above analysis of static concatenation, we can know that the Compilation Program does not know exactly the function to be called at the compilation stage. Only when the program is executed can the function to be called be determined, to this end, You Need To Know exactly the called function and require that the compilation work be performed during the running of the program. Such association work during the running of the program is called dynamic association or dynamic bundle, it is also called the final Association.

Dynamic Association is actually used for dynamic identification. In the preceding example, when static concatenation is analyzed, the objects referenced by S in the fun () function are bundled into the point class. During running, dynamic concatenation sets the s Object Reference to the rectangle class. It can be seen that when an object references S, the bundled class objects in different stages will be different. So how can we determine whether it is static or dynamic Association? C ++ stipulates that dynamic Association is implemented with the support of virtual functions.

From the above analysis, we can see that static and dynamic associations are also polymorphism, which are different choices for different implementations at different stages. In the above example, the implementation is the choice of the polymorphism of the fun () function parameters. The parameter of this function is an object reference of a class. Static Association and Dynamic Association are actually selecting its static type and dynamic type. Association is the choice of polymorphism of this reference.

This is an easier way to understand from another article:

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.

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.