The difference between overloading, overwriting, and hiding in C + +

Source: Internet
Author: User

This digest is from Dr. Lin Rui's "High quality C++/C Programming Guide".

The overloading, overwriting (override) and hiding of member functions are easily confused, and C + + programmers must make sense of the concept, or the error will be hard to find.

1. Overloading and overwriting

Features that are overloaded by member functions:
(1) The same range (in the same class);
(2) The function has the same name;
(3) different parameters;
(4) The virtual keyword is optional.
  overrides refer to a derived class function overriding a base class function, characterized by:
(1) different ranges (in the derived and base classes, respectively);
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword.
In the following example, the function base::f (int) is overloaded with base::f (float), and base::g (void) is overridden by derived::g (void).

1#include <iostream>2 3 classBase4 {5  Public:6     voidFintx) {cout <<"base::f (int)"<< x <<Endl;}7     voidFfloatx) {cout <<"base::f (float)"<< x <<Endl;}8     Virtual voidGvoid) {cout <<"base::g (void)"<<Endl;}9 };Ten  One classDerived: PublicBase A { -  Public: -     Virtual voidGvoid) {cout <<"derived::g (void)"<<Endl;} the }; -  - voidMainvoid) - { + Derived D; -Base *PB = &D; +Pb->f ( the);//base::f (int.) APb->f (3.14f);//base::f (float) 3.14 atPb->g ();//derived::g (void) -}
2. Confusing hidden rules

It is not difficult to distinguish between overloading and overwriting, but the hidden rules of C + + increase the complexity of the problem abruptly. "Hide" here refers to a function of a derived class that masks a base class function with the same name as the following rule:
(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).
In Sample program A:
(1) The function derived::f (float) is covered with base::f (float).
(2) the function derived::g (int) hides base::g (float) instead of overloading.
(3) The function derived::h (float) hides the base::h (float) instead of the overlay.

1 //Example a2 3#include <iostream>4 5 classBase6 {7  Public:8     Virtual voidFfloatx) {cout <<"base::f (float)"<< x <<Endl;}9     voidGfloatx) {cout <<"base::g (float)"<< x <<Endl;}Ten     voidHfloatx) {cout <<"base::h (float)"<< x <<Endl;} One }; A  - classDerived: PublicBase - { the  Public: -     Virtual voidFfloatx) {cout <<"derived::f (float)"<< x <<Endl;} -     voidGintx) {cout <<"derived::g (int)"<< x <<Endl;} -     voidHfloatx) {cout <<"derived::h (float)"<< x <<Endl;} +};

According to the authors, many C + + programmers are unaware of the "hidden" thing. Because the cognition is not deep enough, the occurrence of "concealment" is shadowy and often produces confusing results.
In example B, BP and DP point to the same address, which is supposed to be the same, but that's not the case.

1 //Example B2 3 voidMainvoid)4 {5 Derived D;6Base *PB = &D;7Derived *PD = &D;8 9     //Good:behavior depends solely on type of the objectTenPb->f (3.14f);//derived::f (float) 3.14 OnePd->f (3.14f);//derived::f (float) 3.14 A  -     //Bad:behavior depends on type of the pointer -Pb->g (3.14f);//base::g (float) 3.14 thePd->g (3.14f);//derived::g (int) 3 (surprise!) -  -     //Bad:behavior depends on type of the pointer -Pb->h (3.14f);//base::h (float) 3.14 (surprise!) +Pd->h (3.14f);//derived::h (float) 3.14 -}
3. Get rid of hidden

Hiding the rules caused a lot of trouble. In the example 8-2-3 program, the statement pd->f (10) is intended to call the function base::f (int), but base::f (int) is unfortunately hidden by the derived::f (char *). Because the number 10 cannot be implicitly converted to a string, an error occurs at compile time.

1 classBase2 {3  Public:4     voidFintx);5 };6 7 classDerived: PublicBase8 {9  Public:Ten     voidFChar*str); One }; A  - voidTest (void) - { theDerived *PD =NewDerived; -Pd->f (Ten);//Error -}

From the above example, it seems silly to hide the rule. But there are at least two reasons for the hidden rule to exist:

    • The person writing the statement pd->f (10) may really want to call the Derived::f (char *) function, but he mistakenly writes the parameter incorrectly. With a hidden rule, the compiler can clearly point out the error, which is not necessarily a good thing. Otherwise, the compiler will silently will wrong and the programmer will find it hard to discover this error and shed the curse.
    • If the class derived has multiple base classes (multiple inheritance), it is sometimes unclear which base classes define the function F. If there is no hidden rule, then pd->f (10) may invoke an unexpectedly base class function F. Although hiding rules doesn't seem to make sense, it does kill them.

In the example above, if the statement pd->f (10) must call the function base::f (int), modify the class derived as follows.

1 class  Public Base 2 {3public:4     void F (char *str); 5     void f (int  x) {base::f (x);} 6 };

The difference between overloading, overwriting, and hiding in C + + (GO)

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.