Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Function overloading
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Complications== Two issues interact with and complicate function overloading: [[Name masking]] (due to [[Scope (computer science)|scope]]) and [[implicit type conversion]]. If a function is declared in one scope, and then another function with the same name is declared in an inner scope, there are two natural possible overloading behaviors: the inner declaration masks the outer declaration (regardless of signature), or both the inner declaration and the outer declaration are included in the overload, with the inner declaration masking the outer declaration only if the signature matches. The first is taken in C++: "in C++, there is no overloading across scopes."<ref name=bjarne_faq>{{cite web |url=http://www.stroustrup.com/bs_faq2.html#overloadderived| title=Why doesn't overloading work for derived classes? |last=Stroustrup |first=Bjarne |author-link=Bjarne Stroustrup}}</ref> As a result, to obtain an overload set with functions declared in different scopes, one needs to explicitly import the functions from the outer scope into the inner scope, with the {{code|using}} keyword. Implicit type conversion complicates function overloading because if the types of parameters do not exactly match the signature of one of the overloaded functions, but can match after type conversion, resolution depends on which type conversion is chosen. These can combine in confusing ways: An inexact match declared in an inner scope can mask an exact match declared in an outer scope, for instance.<ref name=bjarne_faq/> For example, to have a derived class with an overloaded function taking a {{code|double}} or an {{code|int}}, using the function taking an {{code|int}} from the base class, in C++, one would write: <syntaxhighlight lang=Cpp> class B { public: void F(int i); }; class D : public B { public: using B::F; void F(double d); }; </syntaxhighlight> Failing to include the {{code|using}} results in an {{code|int}} parameter passed to {{code|F}} in the derived class being converted to a double and matching the function in the derived class, rather than in the base class; Including {{code|using}} results in an overload in the derived class and thus matching the function in the base class.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)