Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report