C + + overload (overload), override (override), redefine (redefine)

Source: Internet
Author: User

C + + due to the same name of the function, several different situations result:

1,overload: Heavy Duty;

Overloading is a way for a function to implement polymorphism, in cases where the function name is the same in the same scope (class). If the function is marked differently, that is, the function has a different number of arguments or types, or the order of the types is different, then the function is overloaded and can invoke different functions according to different parameters.

2,override: rewrite;

Rewriting involves the concept of inheritance, that is, if a function is indicated in the base class is the virtual function, then, in its subclass, if the name of a function is the same as the parameter, then the subclass overrides the virtual function of the base class, and can call the parent class or the child class function according to the different object.

3,redefine: Redefine;

The redefinition may be confused with both of the above. Both overloads and overrides have specific purposes and are implemented in a polymorphic manner. Originally, a subclass is capable of inheriting all the member functions of the parent class, but if the parent class definition is a virtual function, then the subclass can override this function to implement the function with the same function name, but if it is not possible to implement the necessary conditions for overriding: (1) The parent class must be a virtual function (2) The signature and the return Then if a parent class function with the same name is defined in the subclass, then the parent function is hidden and the subclass cannot inherit the function. This includes cases where the parent class is not a virtual function, and the parent function is hidden regardless of whether the signature returns the same value, if the parent class is a virtual function, and the return value of the signature is the same as the override, if the difference is a redefinition.

C + + overload (overload), override (override), redefine (redefine)

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.