C + + basic--overload overload &override overlay &overwrite Hidden

Source: Internet
Author: User

    Overload, override and overwrite English close, more easily confused, coupled with a variety of translations, the use of pigtailed, often is today clear tomorrow confused. These three concepts have been discussed separately in the previous chapters, where the comparison is concentrated to make a memo:

overload(heavy Duty)

The preceding analysis of C + + function overloading is the use of the C + + name mangling mechanism, which allows multiple functions with different parameters of the same name to appear in the same scope, such as:

Class base{

int output (int a) {... };

int output (float B, float c) {... };

}

This is overloaded, features :

1) different parameters of the same name: two functions with the same name are not overloaded, note that the parameters are not const also considered different;

2) only the return value is different, does not constitute the overload;

3) Same scope: Different parameter functions with the same name in the same file domain / global domain /namespace domain can form overloads, and for class member functions, overloads must occur in the same class domain. A function that belongs to a base class and a derived class cannot be directly overloaded (the assertion error of a derived class function overloading a base class function).

override(overwrite)

In a derived class, the virtual function in the implementation base class is redefined, and the process is override(to overwrite), such as:

Class base{

virtual void output () {cout << "base::output ()" << Endl;}

};

Class Drv0:public base{

void output () {cout << Drv0:: Output () "<< Endl;}

};

the drv0::output here is theoverwrite (override) Base::output(someone will mistakenly use the overloaded overload), covering the feature :

1) different scopes (in the derived class and the base class, respectively);

2) The base class function must be the virtual function, and the derived class re-implemented function needs to have the same parameter as the base class virtual function.

Overwrite: Hide (translation is the most chaotic, there are also weighing definitions, overrides, or masks)

Refers to a function of a derived class that masks its base class function with the same name, which occurs between different class domains (derived classes and base classes), including:

1) A derived class has the same name as a base class function but has different parameters, which are hidden by the derived class function regardless of whether the base class function is virtual. (Do not confuse with overloading, overloads occur in the same class of domain)

2) The derived class function has the same parameter as the base class non- virtual function, when the base class function is hidden (not to be confused with overrides, overriding the case that the base class function is virtual )

Post-carding logic: when class public derives, the base class members are all inherited to the derived class, unless the derived class has the same name, when the derived class has the same name as the base class member, except when the base class member is the virtual function (override) , all other cases are hidden.

Distinguish

     1) Overload Principle and override/overwrite have no substantive correlation and similarity , o verload is a horizontal relationship between member functions in the same class; override/overwrite are vertical relationships between derived classes and base classes, whenever a derived class is involved / base class functions cannot be overload . Just overload into overloading, its literal meaning and actual function does not match the image, but implies a vertical relationship between the upper and lower layers, resulting in a derived class covering override or hide overwrite Base class functions are often mistakenly called overloads, so it's easy to overload ( overload " distinguishes it.

2) override overrides the premise or feature is that the base class function must be virtual, the virtual function of the derived class with the same parameter to re-implement the base class is override, After overwriting, you can determine which method is called by the specific object type pointed to by the base-class pointer, thus realizing the function polymorphism, while overwrite ( hidden ) refers to the function of the derived class that masks the base class non-virtual function with the same name. So that it cannot be referenced directly by a derived class object.

The following example shows the difference between covering and hiding:

Class Base

{

virtual void A (int x) {cout << "base::a (int)" << x << Endl; }

void b (int x) {cout << base::b (int) << x << Endl; }

};

Class Drv0:public Base

{

virtual void A (int x) {cout << "drv0::a (int)" << x << Endl; }

void b (int x) {cout << drv0::b (int) << x << Endl; }

};

void Main ()

{

Drv0 D;

Base *pbase = &d;

Drv0 *pdrv = &d;

Pbase->a (3); output drv0::a (int) 3

Pdrv->a (3); output drv0::a (int) 3

pdrv->b (3);// output drv0::b (int) 3

pbase->b (3);// output base::b (int) 3

}

    example drv0::a (int) overrides override base::a (int) , so whether the pbase->a (3) , or call pdrv- >a (3) , which is ultimately based on the object type (drv0 D; Select Call function , the result is drv0::a (int) 3

The drv0::b (int) hides the overwrite base::b (int), and the external representation is based on the pointer type (base* pbase and drv0* pdrv) decide to call the function , i.e. pdrv->b (3) output drv0::b (int) 3, and pbase->b (3) output base::b (int) 3. Thus hiding is equivalent to a function derived class from the first, the base class and the derived class each dry.

so the result of the override is to decide which function to invoke (polymorphic) depending on the type of object, while hiding shows which function to invoke based on the pointer type, which is the most intuitive difference between overriding and hiding .

hiding overwrite is the destruction of function inheritance, which is not necessary in most cases because the non- virtual member function of the base class represents the invariant part of the function of the class, and the derived class should inherit directly without modification, resulting in overwrite majority the description is not well designed.

In fact, it is often careless to implement the override into a overwrite, that is, the base class function should be designed to virtual to achieve variable function, but forgot to write virtual, the result when overriding the same name function in a derived class, it becomes overwrite hidden.

C + + basic--overload overload &override overwrite &overwrite hidden

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.