Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Why should C++ not set different default parameters for the virtual function and its override function?

Shulou Source: shulou.com Published: 2022-06-01 19:17:15 10月01日 Update

This article mainly explains why C++ should not set different default parameters for the virtual function and its overlay function. The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn why C++ should not set different default parameters for the virtual function and its covering function.

C.140: do not set different default parameters for the virtual function and its override function

Reason (reason)

This can cause confusion: the override function does not inherit the default parameters.

Example, bad (negative example) class Base {public: virtual int multiply (int value, int factor = 2) = 0; virtual ~ Base () = default;}

Class Derived: public Base {public: int multiply (int value, int factor = 10) override;}

Derived ditto Base & b = d

B.multiply (10) / / these two calls will call the same function butd.multiply (10); / / with different arguments and so different results

Enforcement (implementation recommendations)

Prompt if the default parameters of the base class and the derived virtual function and override function are different.

Thank you for your reading, the above is the content of "why C++ should not set different default parameters for the virtual function and its coverage function". After the study of this article, I believe you have a deeper understanding of why C++ should not set different default parameters for the virtual function and its coverage function, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Functions parameters differences examples learning content confusion cause reverse that is suggestions ideas situations articles more knowledge knowledge points examples articles follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Tech Info macOS MariaDB Shulou Information