What is the use/advantage of function overloading?
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
- Dynamic Casts or Function Overloads?
相关文章
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- Generics and calling overloaded method from differ
- Should client-server code be written in one “proje
- Algorithm for maximizing coverage of rectangular a
- NameError: name 'self' is not defined, eve
- Overloading a super class's function
- Is there an existing solution for these particular
- Implementation Strategies for Object Orientation
ad-hoc polymorphism is good stuff !!
A function/method is sometimes able to take different kinds of parameters in order to do it's job. This is the time for function overloading. Otherwise, you would have to have different functions for the same functionality, which is confusing and bad practice.
Very valid question.
You get consistency in naming, but at the cost of ambiguity about exact implementation.
The real issue is human memory for method names, right? we find it easier to remember names that are commonly used.
and economy of typing, allowing for shorter method names? fewer different names means (mathematically) that the name itself carries less information.
These two issues shouldn't be any concern, with IDEs that find/guess/insert method names rapidly based on the first few characters, and parameter/return types.
But I do think there is a cost, in preciseness of coding, as well as a benefit.
It providing multiple behaviour to same object with respect to attributes of object.
For example a method called
addUs(a,b)
addsa
andb
.So the definition will be:
But now if you want your arguments to be Objects of a class say:
You want the same function
addUs()
to return a new object which would have attributes height and width having values as sum of the height & width of the 2 arguments passed.So now the definition would be: