Overloading and overwriting and hiding of C + + member functions

Source: Internet
Author: User

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.

The function base::f (int) is overloaded with base::f (float), and base::g (void) is overridden by derived::g (void).

#include <iostream.h>

Class Base

{

Public

void f (int x)

{

cout << "base::f (int)" << x << Endl;

}

void f (float x)

{

cout << "base::f (float)" << x << Endl;

}

virtual void g (void)

{

cout << "base::g (void)" << Endl;

}

};

class derived:public Base

{

PUBLIC:&NBSP;&NBSP;

    virtual void g (void)

     {

        cout << " Derived::g (void) "<< Endl;

   }

};&NBSP;

void Main (void)

{

Derived D;

Base *PB = &d;

Pb->f (42); Base::f (int) 42

Pb->f (3.14f); Base::f (float) 3.14

Pb->g (); Derived::g (void)

}

Hide

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). Sample program 8-2-2 (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.

#include <iostream.h>

Class Base

{

Public

virtual void F (float x)

{

cout << "base::f (float)" << x << Endl;

}

void g (float x)

{

cout << "base::g (float)" << x << Endl;

}

void h (float x)

{

cout << "base::h (float)" << x << Endl;

}

};

Class Derived:public Base

{

Public

virtual void F (float x)

{

cout << "derived::f (float)" << x << Endl;

}

void g (int x)

{

cout << "derived::g (int)" << x << Endl;

}

void h (float x)

{

cout << "derived::h (float)" << x << Endl;

}

};

"Concealment" is a shadowy occurrence, often producing confusing results. In the example below, BP and DP point to the same address, which is supposed to be the same, but that's not the case.

void Main (void)

{

Derived D; Base *PB = &d;

Derived *PD = &d; Good:behavior depends solely on type of the object

Pb->f (3.14f); Derived::f (float) 3.14

Pd->f (3.14f); Derived::f (float) 3.14

Bad:behavior depends on type of the pointer

Pb->g (3.14f); Base::g (float) 3.14

Pd->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

}

Example 8-2-2 (b) overloading, overwriting, and hiding comparisons

8.2.3 get rid of hidden hidden 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.

Class Base {public:void f (int x);};

Class Derived:public Base {public:void f (char *str);};

void Test (void) {Derived *pd = new Derived; pd->f;//Error} Example 8-2-3 cause error due to shadowing

High quality C++/C Programming Guide, v 1.0

2001 Page 101

From the example 8-2-3, it seems silly to hide the rules. But there are at least two reasons for the hidden rule: the person writing the 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.

Example 8-2-3, if the statement pd->f (10) must call the function base::f (int), modify the class Derived as follows. Class Derived:public Base {public:void f (char *str); void f (int x) {base::f (x);}};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Overloading and overwriting and hiding of C + + member functions

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.