Reload, overwrite, and hide member functions

Source: Internet
Author: User

Reprinted: http://www.cnblogs.com/qlee/archive/2011/07/04/2097055.html

It is easy to confuse overload, override, and hide member functions. c ++ programmers must understand
Concept, otherwise the error will be hard to prevent.
1. Heavy Load and coverage
Features of member functions being overloaded:
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
Override refers to the function of a derived class that overwrites the base class function. The features are as follows:
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;
(4) basic functions must have virtual keywords.
In the following example, the base: F (INT) and base: F (float) functions are overloaded with each other, while the base: G (void)
It is overwritten 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:
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 overload and coverage, but the Hidden Rules of C ++ suddenly increased the complexity of the problem.
Here, "hide" means that the function of the derived class shields the base class functions with the same name. The rules are as follows:
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. At this time, no matter whether there is a virtual
Keyword, the function of the base class will be hidden (note not to be confused with the overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual
Keyword. In this case, the function of the base class is hidden (do not confuse with overwrite ).
In the following example:
(1) The derived: F (float) function overwrites base: F (float ).
(2) The derived: G (INT) function hides the base: G (float) instead of the overload.
(3) The derived: H (float) function hides base: H (float) instead of overwrite.
# 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 author's investigation, many c ++ programmers do not realize that there is "hidden. Due to lack of profound understanding,
The occurrence of "hiding" is nothing, and often produces confusing results.
In the following example, BP and DP point to the same address. It is reasonable to say that the running result should be the same.
This is 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
}

3Hide
Hiding rules causes a lot of trouble. In the following example, the intention of the statement Pd-> F (10) is to call the Function
The number base: F (INT), but the base: F (INT) is unfortunately hidden by derived: F (char. Because the number is 10
It cannot be implicitly converted into a string, so an error occurs during compilation.
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.