| Start |
|---|
| What is Function Overloading? Function overloading is having multiple functions declared in the same scope with the exact same name exist in the same place (known as scope) differing only in their signature, meaning the arguments they accept. Suppose you are writing a series of functions for generalized printing capabilities, beginning with This works fine, but suppose you want a function that also accepts an But because the two functions accept different parameters, you can simply write: Now you have 2 functions, both named Instead of: When you have overloaded functions, the compiler infers which of the functions to call from the parameters you provide it. Care must be taken when writing function overloads. For example, with implicit type conversions: Now it's not immediately clear which overload of And you might need to give your compiler some clues, like: Some care also needs to be taken when writing overloads that accept optional parameters: Because there's no way for the compiler to tell if a call like |
| Return Type in Function Overloading Note that you cannot overload a function based on its return type. For example: This will cause a compilation error as the compiler will not be able to work out which version of |
| Member Function cv-qualifier Overloading Functions within a class can be overloaded for when they are accessed through a cv-qualified reference to that class; this is most commonly used to overload for This is necessary because a member function can only be called if it is at least as cv-qualified as the instance it's called on. While a non- A class with some basic This is a key tenet of A common usage of this is declaring accessors as No class members can be modified within a RemarksAmbiguities can occur when one type can be implicitly converted into more than one type and there is no matching function for that specific type. For example: |
Read Article
https://develop.horje.com/learn/1434/reference