Why should C++ not set different default parameters for the virtual function and its override function?
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!