Questions tagged [method-overloading]
38 questions
62
votes
10 answers
Is it bad practice to use a C++ compiler just for function overloading?
So I am working on a software design using C for a certain processor. The tool-kit includes the ability to compile C as well as C++. For what I am doing, there is no dynamic memory allocation available in this environment and the program is overall…

Snoop
- 2,718
- 5
- 24
- 52
41
votes
2 answers
Why PHP doesn't support function overloading?
I am wondering if one of the key features of a programming language is to have the ability to overload functions via arguments. I think it is essential in-context of the Object oriented programming.
Is it intentionally left behind and not allowed?…

user1179459
- 1,183
- 3
- 13
- 18
36
votes
5 answers
Is it enough for methods to be distinguished just by argument name (not type)?
Is it enough for methods to be distinguished just by argument name (not type) or is it better to name it more explicitly?
For example T Find(int id) vs T FindById(int id).
Is there any good reason to name it more explicitly (i.e. adding ById)…

Konrad
- 1,529
- 2
- 17
- 32
36
votes
2 answers
Why do methods that take an unlimited amount of parameters often define overloads with fewer parameters?
For instance, the System.IO.Path.Combine method in .NET has the following overloads:
Combine(params String[])
Combine(String, String)
Combine(String, String, String)
Combine(String, String, String, String)
What is the point of the last three?
The…

Alex
- 470
- 3
- 7
19
votes
12 answers
Is method overloading anything more than syntactic sugar?
Is method overloading a type of polymorphism? To me it seems like simply the differentiation of methods with the same name and different parameters. So stuff(Thing t) and stuff(Thing t, int n) are entirely different methods as far as the compiler…

Aviv Cohn
- 21,190
- 31
- 118
- 178
17
votes
4 answers
Should we rename overloaded methods?
Assume an interface containing these methods :
Car find(long id);
List find(String model);
Is it better to rename them like this?
Car findById(long id);
List findByModel(String model);
Indeed, any developer who use this API won't need to…

Mik378
- 3,838
- 7
- 33
- 60
15
votes
5 answers
Why isn't the overloading with return types allowed? (at least in usually used languages)
I don't know about all programming languages, but it's clear that usually the possibility of overloading a method taking into consideration its return type (assuming its arguments are the same number and type) is not supported.
I mean something like…

user2638180
- 383
- 2
- 8
13
votes
3 answers
Does one method overload an other, or are both methods "overloaded"
If I create this method
public void foo()
And then I create an overloaded version like this
public void foo( string bar )
Do we say that the second functions overloads the first, or are both methods equally "overloaded"?
This would imply (I…

Willem D'Haeseleer
- 232
- 1
- 8
13
votes
1 answer
Why is there no facility to overload static properties in PHP?
Intro
PHP allows you to overload method calls and property accesses by declaring magic methods in classes. This enables code such as:
class Foo {
public function __get($name) { return 42; }
}
$foo = new Foo;
echo $foo->missingProperty; //…

Jon
- 527
- 3
- 15
11
votes
2 answers
Overload or Optional Parameters
When I have a function that might, or might not receive a certain parameter, is it better to overload the function, or to add optional args?
If each one has it's ups and downs - when would I use each?

JNF
- 399
- 1
- 5
- 17
11
votes
3 answers
When is method overloading appropriate?
Suppose I am working on an existing, reasonably large system. I have an object, myObject of class MyClass (for the example's sake, suppose I'm working in Java). myObject is a composition containing a Collection, say, a List and other objects which…

blahman
- 386
- 1
- 3
- 14
8
votes
4 answers
How to prevent duplicate data access methods that retrieve similar data?
In almost every project I work on with a team, the same problem seems to creep in. Someone writes UI code that needs data and writes a data access method:
AssetDto GetAssetById(int assetId)
A week later someone else is working on another part of…

Ronald Wildenberg
- 215
- 1
- 6
7
votes
3 answers
Overloading methods that do logically different things, does this break any major principles?
This is something that's been bugging me for a bit now. In some cases you see code that is a series of overloads, but when you look at the actual implementation you realize they do logically different things. However writing them as overloads allows…

siva.k
- 181
- 3
6
votes
6 answers
How to resolve methods with the same name and parameter types?
In many cases, I want to write methods that have the same functionality for different types of inputs. This is easily accomplished by method overloading if the parameter types are different.
But what's the best (most robust) way to go about…

tskuzzy
- 732
- 1
- 7
- 12
5
votes
1 answer
Is tag dispatch as used in CppCoreGuidelines T.65 antiquated?
The CppCoreGuidelines contain the following:
T.65: Use tag dispatch to provide alternative implementations of a function
[...]
Example
struct pod_tag {};
struct non_pod_tag {};
template struct copy_trait { using tag = non_pod_tag;…

Jan Schultke
- 159
- 2