A summary of the differences between overloading overwriting and hiding class member functions

  • 2020-04-02 01:41:44
  • OfStack

The answer:
A. Features of overloaded member functions:
(1) the same scope (e.g. in the same class);
(2) the function name is the same;
(3) different parameters;
(4) virtual keywords are optional.

B. Override refers to the function of derived class overriding the function of base class, with the following characteristics:
(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.

C. "hidden" means that a function of a derived class shields a function of its 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 (note not to be confused with overrides)

Hiding breaks polymorphism, so it is common to replace hiding with overwriting.


Related articles: