Hiding and overwriting in C ++

Source: Internet
Author: User

Overload, override, and hiding of member functions are very confusing. c ++ programmers must understand the concept, otherwise errors 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
Keyword is 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
Keyword.
Example 1
, Function base: F (INT)
And base: F (float)
Load each other, while base: G (void)
It is overwritten by derived: G (void.

# Include <iostream>

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)
}

Example 1 overload and overwrite of a member function

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 Example 2 (:
(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>

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

Example 2 (a) overload, overwrite, and hide a member function

According to the author's investigation, many c ++ programmers do not realize that there is "hidden. Due to lack of deep understanding, the occurrence of "hiding" is a real failure and often produces confusing results.

In Example 2 (B), BP
It points to the same address as DP, and the running result should be the same, but 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
}

Example 2 (B) Comparison of overload, overwrite, and hide

3. Get rid of hiding
Hiding rules causes a lot of trouble. Example 3
In the program, the intention of the statement Pd-> F (10) is to call the function base: F (INT), but the base: F (INT) is unfortunately
Derived: F (char *) hidden. 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
}

Example 3 error caused by hiding

In Example 3, hiding a rule seems silly. However, there are at least two reasons for hiding rules:
The person who writes the statement Pd-> F (10) may really want to call the derived: F (char *) function, but he mistakenly writes the parameter wrong. With the hidden rules, the compiler can clearly point out errors. This is not necessarily a good thing. Otherwise, the compiler will quietly correct the error, and it will be difficult for programmers to find this error and cause a curse.
Assume that the class derived
There are multiple base classes (Multi-inheritance), and sometimes it is unclear which base classes define function f. If no rule is hidden, Pd-> F (10) may call an unexpected base class function.
F. Although hiding rules does not seem reasonable, they can indeed eliminate these accidents.
In Example 3, if the statement Pd-> F (10) must call the function base: F (INT ),
Modify derived to the following.

Class derived: public Base
...{
Public:
Void F (char * Str );
Void F (int x)... {base: F (x );}
};

It is actually a problem of scope. The hidden scope is wider, as long as it is different scope, it can be hidden. Hiding involves a wider range. There is a hidden concept in C language.
Overwrite all parent and child classes with virtual functions with the same name as the parameter, which is much more demanding.
This is actually two completely different concepts. There is nothing to do with it, but it just shows a little similar. Each of them has its own usage conditions and environment. If they are combined into one, there is no reason to combine them into one. In fact, there are many differences between them using a unified concept. No way or no reason to do this

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.