[Reprinted] override overload hide Analysis

Source: Internet
Author: User

The overloading function in C ++ is "context-free overloading" and must have different parameter lists. Ada and lisp support "context-dependent overloading", which can be considered as an overload if the returned value is different.

 

These three concepts are related to polymorphism in OO. It is easier to distinguish between overload and coverage, but hiding this concept makes the problem a little complicated. Let's talk about their differences.

Overload oveloadIt means that different functions use the same function name, but the function parameters are different (the return values can be the same or different ). Different functions are called according to their parameters. Overload refers to being in the same scope as a class.

Override(Also called rewriting) refers to re-implementing the virtual function (note that it is a virtual function) in the base class in the derived class. That is, the function names and parameters are the same, but the function implementation bodies are different. Overwrite refers to different scopes, such as base classes and subclasses.

Hide hideA function in a derived class shields non-virtual functions with the same names in the base class. ForNon-virtual typeIf its subclass also declares a function with the same name as the function, then the function in the base class (maybe a series of functions, if the function is overloaded in the base class, will be hidden,It can be called through the domain resolution operator..

Reference other people's code to illustrate the problem (reference from Lin Rui's "High Quality C/C ++ programming guide").

 

Take a closer look at the following code:

 

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

};

 

Do you see anything? It is described below:

 

(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.

 

Well, I understand the concept, but what problems do we encounter in actual programming? Let's look at the following code:

 

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, which depends on the concept of the domain, because Pb is a pointer to the base

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

}

 

In the first call, the action of a function depends on the object pointed to by the pointer. In the second and third calls, the action of a function depends on the pointer type. It is actually caused by the scope. Therefore, hiding destroys the polymorphism in object-oriented programming, which can lead to confusion for OOP personnel.

However, hiding is not useless. It can help programmers find some wrong calls during compilation. However, I think we should try to hide these features as much as possible. Add them when adding virtual.

 

Original article addressHttp://blog.csdn.net/yanjun_1982/archive/2005/09/02/470405.aspx

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.