C++ of static and dynamic linkage

  • 2020-04-02 02:30:40
  • OfStack

I. overview:

Generally speaking, linking is the process of combining modules or functions to generate executable code, and allocating the memory address for each module or function call, as well as the correct memory address for external access. It is the process of associating computer programs with each other. According to the different stages of linkage, it can be divided into two different methods: Static binding and Dynamic binding .

Static binding refers to the compilation phase will function and the function call, so the static binding is also called the early binding, in the compile phase, you must know all the function or module performs the information they need to detect, it's choice of function is based on the type of a pointer to the object (or reference), C language, all of the binding are static binding, and any kind of static binding compiler support.

Dynamic linking refers to the function implementation and function call will be associated when the program is executed, so it is also called runtime binding or late binding. The function selection of dynamic linking is not based on Pointers or references, but on object types, different object types will make different compilation results. In C++, linking is normally static, but when it comes to polymorphic and virtual functions, you have to use dynamic linking. Now let's talk about polymorphism.

Polymorphism: the literal meaning is to have many forms or forms. C++ polymorphism has two forms, dynamic polymorphism and static polymorphism ; Dynamic polymorphism refers to the general polymorphism, which is realized by class inheritance and virtual function mechanism. Static polymorphism is implemented through a template, and is called static polymorphism because it is not run at compile time.

Ii. Code examples

Examples of dynamic polymorphism are as follows:


#include <stdio.h>
#include <iostream>

class CShape
{
public:
 CShape(){}
 virtual ~CShape(){}
 virtual void Draw() = 0;
};

class CPoint : public CShape
{
public:
 CPoint(){}
 ~CPoint(){}
 void Draw()
 {
 printf("Hello! I am Point!/n");
 }
};

class CLine : public CShape
{
public:
 CLine(){}
 ~CLine(){}
 void Draw()
 {
 printf("Hello! I am Line!/n");
 }
};

void main()
{
 CShape* shape = new CPoint();
 //draw point
 shape->Draw();//Here shape will call the Draw() function of CPoint
 delete shape;
 shape = new CLine();
 //draw Line
 shape->Draw();//Here shape will call CLIne's Draw() function
 delete shape;
 return ;
}

You can consider its output, if you really do not know, then please run it!
From the above example, you should be able to understand what polymorphism is: a Draw () can be implemented in two ways, and is determined at runtime, not known at compile time, and never known! Only when we run can we know what kind of shape we are producing. Of course, to achieve this effect, we need to link dynamically. In the base class, we will declare functions that we want to be polymorphic as virtual functions. If you want to know more thoroughly, then check the information on the Internet! I won't bother to explain how virtual functions work here.

Here is also an example of static polymorphism:

Add template functions on top of 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 point
 shape->Draw();
 DrawShape<CPoint>((CPoint*)shape);
 delete shape;
 shape = new CLine();
 //draw Line
 shape->Draw();
 DrawShape<CLine>((CLine*)shape);
 delete shape;
 
 return ;
}

When the program compiles the main function, the compiler has already specified which implementation Draw in the DrawShape function will call. This is static polymorphism, and the function to call is known at compile time.


Related articles: