Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the compile-time polymorphism and run-time polymorphism of C++?

Shulou Source: shulou.com Published: 2022-06-03 05:26:55 09月27日 Update

What is C++ compile-time polymorphism and run-time polymorphism? in order to solve this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Today's C++ is no longer a simple "C with class" language, it has developed into a collection of languages composed of multiple sublanguages, among which generic programming and STL based on it are the most outstanding parts of C++ 's development. In object-oriented C++ programming, polymorphism is one of the three characteristics of OO, which is called run-time polymorphism, also known as dynamic polymorphism; in generic programming, polymorphism is based on the realization of template (template) and overloaded parsing of functions, and this polymorphism occurs at compile time, so it is called compile-time polymorphism or static polymorphism.

Run-time polymorphism

The design idea of run-time polymorphism should be attributed to the design of class inheritance system. For the collection of objects with related functions, we always want to be able to abstract the set of functions they share, declare these functions as virtual interfaces (virtual functions) in the base class, and then rewrite these virtual interfaces by inheriting the base class from the subclass. to achieve the specific functions unique to the subclass. Canon

Class Animal

{

Public:

Virtual void shout () = 0

}

Class Dog: public Animal

{

Public:

Virtual void shout () {cout shout ()

/ / delete object

...

Return 0

}

The implementation of runtime polymorphism depends on the virtual function mechanism. When a class declares a virtual function, the compiler will place a virtual function table pointer for the class object and set a unique virtual function table for the class, where the virtual function address is stored. During the operation, the real realization of this kind of virtual function is determined by virtual function table pointer and virtual function table.

The advantage of run-time polymorphism is that it makes it possible to deal with sets of heterogeneous objects:

/ / We have a zoo with a bunch of animals in it

Int main ()

{

Vectoranims

Animal * anim1 = new Dog

Animal * anim2 = new Cat

Animal * anim3 = new Bird

Animal * anim4 = new Dog

Animal * anim5 = new Cat

Animal * anim6 = new Bird

/ / dealing with heterogeneous sets

Anims.push_back (anim1)

Anims.push_back (anim2)

Anims.push_back (anim3)

Anims.push_back (anim4)

Anims.push_back (anim5)

Anims.push_back (anim6)

For (auto & I: anims)

{

I-> shout ()

}

/ / delete object

/ /...

Return 0

}

Summary: runtime polymorphism occurs at run time through virtual functions

Compile-time polymorphism

For template parameters, polymorphism is realized by template realization and function overload parsing. The realization of different template parameters leads to the call of different functions, which is called compile-time polymorphism. Compared with run-time polymorphism, classes that implement compile-time polymorphism do not need to be an inheritance system, and there can be no relationship between them, but the constraint is that they all have the same implicit interface. Let's rewrite the above example as follows:

Class Animal

{

Public:

Void shout () {cout

Tags: Polymorphisms functions compilers interfaces lines templates objects programming compilers parameters programming different functional heterogeneous processing running systems animals that is hierarchies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple MariaDB Redmi Shulou Information