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 method of overloading, hiding and overwriting C++ function

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the C++ function reloading, hiding and overwriting method is what the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that after reading this C++ function reloading, hiding and overwriting method is what the article will gain, let's take a look.

1 function overload

Definition:

C++ stipulates that in the same scope, for example, among the member functions of a class, multiple functions have the same name, but the formal parameters of each function (that is, the number, type or order of parameters) are different, resulting in function overloading.

Code example

Int test (int a); int test (int a, double b); int test (double b, int a); int test (int a, const char * * c); void test (int a, const char * * c); / / non-overloaded, compilation will prompt an error, and only return values that different compilers cannot distinguish between the overloaded function

Summary

Premise: the function name is the same, that is, the function with the same name is required.

Overloading scope: function overloading occurs in the same scope at the horizontal level, such as overloading between class member functions and between global functions

Overloaded type: whether it is a static member function of a class, a normal member function of a class, or an ordinary function, it can be overloaded.

Overloaded elements: function return value type function overloading has nothing to do with overloading, only if the returned values are different and the formal parameters are the same, overloading will be prohibited

2 function hiding

Define

Function hiding means that defined functions with the same name constitute function hiding in different scopes (only requiring the same function name, no more requirements for return values and formal parameters, and no requirements for whether they are virtual functions or not). For example, the derived class member function of the same name masks the member function of the same name as its base class, and masks the global external function of the same name. There is often confusion between hiding and overwriting rewriting, so in advance, if there is a virtual function with the same name as the base class in the derived class, and the return value and parameters are the same, it constitutes a function rewriting.

Code example

# include using namespace std;class Parent {public: void test (int a) {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