C++ concept of overloading overwriting hidden instructions

  • 2020-04-01 23:29:06
  • OfStack

Function overload:

In C++ programs, you can use the same name for semantically similar functions, that is, function overloading.

Implementation of overloading:

Several overloaded functions with the same name are still different functions. How do they differ? We naturally think of two elements of a function interface: arguments and return values. If the arguments to the function of the same name are different (including type and order), it is easy to distinguish them from each other.

Characteristics of overloaded and overridden member functions:

(1) same scope (in the same class);

(2) the function name is the same;

(3) different parameters;

(4) virtual keywords are optional.

Overwrite refers to the derived class function overwrite the base class function, the characteristics are:

(1) different scope (respectively in derived class and base class);

(2) the function name is the same;

(3) the parameters are the same;

(4) the base class function must have a virtual keyword.

Hidden rules: it's not difficult to distinguish between overloading and overwriting, but C++ 's hidden rules add a lot of complexity. Here, "hidden" means that the functions of derived classes mask the functions of the base class with the same name. The rule is as follows:

(1) if the function of the derived class is the same as the function of the base class, but the parameters are different. At this point, the functions of the base class are hidden with or without the virtual keyword (be careful not to be confused with overloading).

(2) if the function of the derived class is the same as the function of the base class and the parameters are the same, but the base class function does not have a virtual keyword. At this point, the functions of the base class are hidden (be careful not to be confused with overrides).

 


Related articles: