Overload, override, and overwrite in C + +

Source: Internet
Author: User

The notion that there is a set of fundamentals in the C + + language is always confusing: What does overload, override, and overwrite mean? Here are three concepts to organize:

1. Overload (Heavy Duty)

The notion of overloading is best understood, in the same class declaration scope, where multiple functions with identical names and different parameters (types or numbers) are defined, called Overload (Overloads). The characteristics of overloading are as follows:

(1) the same range (in the same class);
(2) the function has the same name;
(3) different parameters;
(4)The virtual keyword is optional.

2. Override (Overwrite)

The concept of overriding is actually used to implement C + + polymorphism, where subclasses rewrite functions declared by the parent class as virtual. The override (overwrite) features are as follows:

(1) different ranges (in the derived and base classes, respectively);
(2) the function has the same name;
(3) The parameter list is identical;
(4) the base class function must have the virtual keyword.

3. Overwrite (rewrite)

Rewriting refers to a function mask (or "hidden") of a derived class that has a base class function with the same name. It is this C + + hidden rule that causes the complexity of the problem to increase abruptly, which is divided into two situations:

(1) if the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (note that it is not confused with overloading).
(2) if the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay).

Take a look at an online example to see overwrite (rewrite) situation:

#include <iostream>using namespacestd;classbase{ Public:    Virtual voidFfloatx) {cout <<"base::f (float)"<< x <<Endl;} Virtual voidGfloatx) {cout <<"base::g (float)"<< x <<Endl;} voidHfloatx) {cout <<"base::h (float)"<< x <<Endl;}};classDerived: Publicbase{ Public:    Virtual voidFfloatx) {cout <<"derived::f (float)"<< x <<Endl;} Virtual voidGintx) {cout <<"derived::g (int)"<< x <<Endl;} voidHfloatx) {cout <<"derived::h (float)"<< x <<Endl;}};intMain () {Derived D; Base*PB = &D; Derived*PD = &D; //Good:behavior depends solely on type of the objectPb->f (3.14f);//derived::f (float) 3.14Pd->f (3.14f);//derived::f (float) 3.14//Bad:behavior depends on type of the pointerPb->g (3.14f);//base::g (float) 3.14 (surprise!)Pd->g (3.14f);//derived::g (int) 3//Bad:behavior depends on type of the pointerPb->h (3.14f);//base::h (float) 3.14 (surprise!)Pd->h (3.14f);//derived::h (float) 3.14    return 0;}
View Code

In the example above:

    • The function derived::f (float) overrides Base::f (float).
    • The function derived::g (int) hides the base::g (float).
    • The function derived::h (float) hides base::h (float).

4. Special Case Notes

In addition to the three cases mentioned above, there are some more confusing places, such as:

4.1 The common function of the same name and the Const function are essentially two different functions, which should be understood as different from the arguments of the two functions with the same name. There may be misunderstandings in the understanding of virtual functions in derived classes.

See the example below:

#include <iostream>using namespacestd;classbase{ Public:    Virtual voidFfloatx) {cout <<"base::f (float)"<< x <<Endl;}};classDerived: Publicbase{ Public:    Virtual voidFfloatXConst{cout <<"derived::f (float)"<< x <<Endl;}};intMain () {Derived D; Base*PB = &D; Derived*PD = &D; //Bad:behavior depends solely on type of the objectPb->f (3.14f);//base::f (float) 3.14Pd->f (3.14f);//derived::f (float) 3.14    return 0;}
View Code

4.2 Virtual virtual function defined in the base class, in the inheriting subclass, the same name function automatically belongs to the virtual function, you can not need the virtual keyword.

4.3 If the function defined in the base class is not virtual, and the same function is defined as virtual in the subclass, it is called offside, and the function behavior depends on the type of the pointer/reference, not the type of the actual object.

  See the example below:

#include <iostream>using namespacestd;classbase{ Public:    voidF () {cout <<"base::f ()"<<Endl;} Virtual voidG () {cout <<"base::g ()"<<Endl;}};classDerived: Publicbase{ Public:    Virtual voidF () {cout <<"derived::f ()"<<Endl;} voidG () {cout <<"derived::g ()"<<Endl;}};classVirtualderived:Virtual  Publicbase{ Public:    voidF () {cout <<"virtualderived::f ()"<<Endl;} voidG () {cout <<"virtualderived::g ()"<<Endl;}};intMain () {Base*d =NewDerived; Base*VD =Newvirtualderived; D->f ();//base::f () bad behaviorD->g ();//derived::g ()VD->f ();//base::f () bad behaviorVd->g ();//virtualderived::g ()Delete D;    Delete VD; return 0;}
View Code

5. Description of inheritance for non-virtual functions

The rule in effective C + + is to prohibit the redefinition of inherited non-virtual functions under any conditions.

The meaning of public inheritance is "Yes " (is a), "declaring a non-virtual function in a class actually creates a special invariance on this class." If you apply these analyses to Class B, Class D, and non-virtual member function B::MF, then:

  (1) All that applies to the B object also applies to the D object, because each D object is "a" B object.
  (2) The subclass of B must inherit both the interface and the implementation of MF, since MF is a non-virtual function in B.

Then, if D redefined MF, there would be a contradiction in the design. If d really needs to implement and B different MF, and each B object (no matter how special) also really want to use the B implementation of MF, then each d will not be "a" B. In this case, D cannot inherit from B public. Conversely, if D really must inherit from B, and D really needs a different MF implementation than B, then MF does not reflect the invariance of specificity for B. In this case, MF should be a virtual function. Finally, if each d is really "a" B, and if MF really creates a specific invariance for B, then D actually does not need to redefine MF, and it must never do so.

Whichever of the above arguments can be used to conclude that it is forbidden to redefine inherited non-virtual functions under any conditions.

Overload, override, and overwrite in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.