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 are the advantages and disadvantages of compiler polymorphism and runtime polymorphism in C++

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the advantages and disadvantages of compiler polymorphism and runtime polymorphism in C++. What is introduced in this article is very detailed and has certain reference value. Interested friends must read it!

Run-time polymorphism

The design idea of run-time polymorphism should be attributed to the design idea of class inheritance system. for the collection of objects with related functions, we always hope to abstract the set of functions they share. These functions are declared as virtual interfaces (virtual functions) in the base class, and then these virtual interfaces are rewritten by the subclass inheritance class to realize the unique functions of the subclass.

Summary: runtime polymorphism occurs at run time through virtual functions

Advantages

Important characteristics in design, intuitive understanding of the objective world

Able to deal with heterogeneous sets under the same inheritance system

Shortcoming

Binding virtual functions during the run time increases the runtime overhead of the program.

There is a huge class inheritance hierarchy, and the modification of the interface can easily affect the class inheritance hierarchy.

Because the virtual function is determined at runtime, the compiler cannot optimize the virtual function

Compiler polymorphism

For template parameters, polymorphism is achieved through template specific and function overloading, and different templates cause different functions to be called, which is compiler polymorphism. Compared with run-time polymorphism, classes between compiler polymorphisms do not need an inheritance system, and they are constrained by implicit interfaces.

Shortcoming

Code debugging brings difficulties

It is impossible to separate the compilation of templates. When the project is very large, the compilation time should not be underestimated.

Unable to process a collection of heterogeneous objects

Noun interpretation

Function rewriting: when a subclass defines a virtual function exactly the same as the parent class, the function of the subclass overrides (also known as overriding) the function of the parent class.

These are all the contents of the article "what are the advantages and disadvantages of compiler polymorphism and runtime polymorphism in C++?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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