Analysis of overloading, rewriting and redefinition

Overloading, rewriting, redefinition comparison

Insert image description here

1. Overload

means that the function name is the same, but the number or order and type of its parameter list are different. But cannot be judged by the return type.
(1) In the same scope
(2) The function name is the same
(3) The parameter list is different (parameters Number, parameter type, type order...)
(4) The virtual keyword is optional
(5) The return values ​​can be the same or different a>

2. Rewrite/override

means that the derived class has a virtual function that is the same as the base class (return value type (except covariance), function name, parameter list). The characteristics are:
(1) The two virtual functions are located in the derived class and the base class respectively
(2) The function names are the same
(3) The parameter lists are the same
(4) The base class function must add the virtual keyword in front of the function and cannot have static
(5) The return value is the same (except for covariance) < a i=7> (6) The access modifiers of overridden functions can be different (The overridden function in the derived class can relax its access modifier in the base class, although the base class virtual function It is private. It is also possible to override it in a derived class and rewrite it as public or protected.)

3. Redefining/hiding (redefining)

(1) The two functions are located in the derived class and the base class respectively
(2) The function names are the same
(3) The return values ​​can be different a>. The two functions with the same name are located in the derived class and the base class respectively. They constitute either overwriting or redefinition (6) Hidden functions do not have polymorphism. (5) The parameters are the same, but the base class function does not have the virtual keyword. At this time, the functions of the base class are hidden.
(4) The parameter list is different. At this time, regardless of the virtual keyword, the base class function will be hidden (be careful not to confuse it with overloading. First, the scope is different, and the overloading must be in the same scope).


* Why can't the static keyword be used in virtual functions?

Why can’t the static keyword be used in virtual functions?
This needs to consider the characteristics of both——

In C++, virtual functions and static member functions have different semantics and purposes, so they are syntactically mutually exclusive.

Dynamic Binding

A key feature of virtual functions is dynamic binding (also known as runtime polymorphism). When a virtual function is called through a base class pointer or reference, the derived class version is actually called. This decision is made at runtime based on the actual type of the object. >Processing - This mechanism allows the program to perform the operations we want based on the actual type of the object.

static member function

Static member functions belong to the class rather than to instances of the class.
Static member functions are shared throughout the class.
Static member functions are resolved at compile time because they do not depend on the actual type of the object.

conclusion

Since virtual functions and static member functions have different calling and resolution mechanisms, combining them can lead to confusion.

In C++, virtual functions are implemented through virtual function tables and virtual pointers, while static member functions do not involve these mechanisms~

Therefore, in order to maintain language consistency and clarity, C++ stipulates that the static keyword cannot be used in virtual function declarations. When using virtual functions, the compiler will use the dynamic binding mechanism, and static member functions do not participate in this mechanism, so using static in virtual functions is inappropriate

* Can the access modifier of an overridden function be different?

When in a derived class overrides the virtual function in the base class, the access modifiers of the functions in the derived class can have some changes. But there are some restrictions:

Narrow access

A function that is overridden in a derived class can narrow its access modifiers in the base class.

  1. If a virtual function in the base class is public, you can choose to override it as public, protected, or private in the derived class.
  2. If a virtual function in the base class is protected, you can choose to override it as protected or private in the derived class.
  3. If a virtual function in a base class is private, it cannot be overridden directly in a derived class.
    Insert image description here

Widen access

Functions overridden in a derived class cannot relax their access modifiers in the base class.

For example, if a virtual function in a base class is private, it cannot be made public when overriding it in a derived class.

Insert image description hereInsert image description here

Guess you like

Origin blog.csdn.net/m0_74195626/article/details/134712915