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

Example Analysis of Polymorphism, Virtual function and override specifier in Category 11

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

Share

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

This article mainly introduces the example analysis of polymorphisms and virtual functions and override specifiers in Clover 11. It is very detailed and has a certain reference value. Interested friends must read it!

Polymorphisms and imaginary functions

Polymorphism is not only one of the important characteristics of C++, but also difficult for beginners to understand. Let's start with a simple example:

The code is simple. To describe the main points of polymorphism from definition to function in the above code, according to the author's own words, it should not be any easier, starting with the main function:

Pointer to the base class

An object that points to a derived class

The derived class overrides the virtual function defined in the base class

These three points are necessary and indispensable.

There are other interesting things about the above code, which doesn't affect the result, but it's better to know about it. When you redefine (override) a virtual function in the base class in a derived class:

The virtual keyword is not a must

It is not necessary to declare the function as public

Override a failed situation

The coverage of virtual functions requires that the signatures of the two functions must be the same. For example, the following code:

Polymorphism cannot be implemented because the signature of sub2::f1 is not consistent with that of base::f1.

You may think this mistake is too obvious to happen at all. That's because the member function in this example is too simple. In the actual development, with the expansion of the development scale, the inheritance relationship of the class will become deeper and deeper, and the parameters of the member function will become more and more. It is often encountered that the signature of the member function defined in the derived class is inconsistent with the signature of the overlay object, which leads to the overwrite failure.

And fatally, this kind of error does not produce compilation errors and is not easy to find.

Override specifier

To solve this problem, override specifier is used when declaring and defining override functions in derived classes.

Because of the clear intention of the function, a compilation error occurs when the compiler cannot find a virtual function with the same signature in the base class.

The above is all the contents of the article "sample Analysis of Polymorphisms and Virtual functions and override specifiers in Category 11". 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