Heavy load, overlay and hide of member functions detailed parsing _c language

Source: Internet
Author: User

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

Copy Code code as follows:

#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
virtual void g (void) {cout << "derived::g (void)" << Endl}
};
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)
}


2 confusing hidden rules
It was not difficult to distinguish between overloads and overrides, but the hidden rules of C + + caused the complexity of the problem to increase precipitously.
Here "hiding" means that a function of a derived class masks a base class function with the same name as the following:

(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 you are 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 functions of the base class are hidden (note that you are confused with overrides).

In the following example:
(1) function derived::f (float) covers base::f (float).
(2) function derived::g (int) hides base::g (float) instead of overloading.
(3) function derived::h (float) hides base::h (float) instead of overwriting it.

Copy Code code as follows:

#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;}
};

According to the authors, many C + + programmers are unaware of the "hidden" thing. Because the understanding is not deep enough, the "hidden" occurrence is shadowy, often produces the puzzling result.

The following example, BP and DP point to the same address, the results should be the same, but the fact is not the case.

Copy Code code as follows:

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
}

3 Get rid of hidden
Hiding the rules caused a lot of trouble. In the following example, the statement pd->f (10) is intended to invoke a function base::f (int), but base::f (int) is unfortunately hidden by derived::f (char *). Because the number 10 cannot be implicitly converted to a string, an error occurred at compile time.
Copy Code code as follows:

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 (10); Error
}

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.