Questions tagged [overloading]

A type of polymorphism where different functions with the same name or different implementations of an operator are invoked based on the data types of the parameters passed. DO NOT USE FOR OVERLOADING IN PHP; THE MEANING OF THAT TERM IN PHP IS UNRELATED.

The terms overloading and overloaded may refer to:

Don't confuse it with

Related tags:

6857 questions
160
votes
7 answers

How to do method overloading for null argument?

I have added three methods with parameters: public static void doSomething(Object obj) { System.out.println("Object called"); } public static void doSomething(char[] obj) { System.out.println("Array called"); } public static void…
Phani
  • 5,319
  • 6
  • 35
  • 43
156
votes
9 answers

Overriding == operator. How to compare to null?

Possible Duplicate: How do I check for nulls in an ‘==’ operator overload without infinite recursion? There is probably an easy answer to this...but it seems to be eluding me. Here is a simplified example: public class Person { public string…
Flipster
  • 4,373
  • 4
  • 28
  • 36
146
votes
14 answers

What is the difference between dynamic and static polymorphism in Java?

Can anyone provide a simple example that explains the difference between Dynamic and Static polymorphism in Java?
Prabhakar Manthena
  • 2,223
  • 3
  • 16
  • 30
142
votes
12 answers

Default parameters with C++ constructors

Is it good practice to have a class constructor that uses default parameters, or should I use separate overloaded constructors? For example: // Use this... class foo { private: std::string name_; unsigned int age_; public: foo(const…
Rob
  • 76,700
  • 56
  • 158
  • 197
140
votes
4 answers

Why does String.valueOf(null) throw a NullPointerException?

according to the documentation, the method String.valueOf(Object obj) returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned. But how come when I try do…
user282886
  • 3,125
  • 8
  • 22
  • 11
137
votes
11 answers

What is function overloading and overriding in php?

In PHP, what do you mean by function overloading and function overriding. and what is the difference between both of them? couldn't figure out what is the difference between them.
Parag
  • 4,754
  • 9
  • 33
  • 50
136
votes
10 answers

Overload with different return type in Java?

Why is it not possible to overload a function just by changing the return type? Will that change in a future version of Java? By the way, just for reference, is this possible in C++?
nunos
  • 20,479
  • 50
  • 119
  • 154
135
votes
3 answers

Method overloading in Objective-C?

As far as my knowledge, Objective-C does not support method overloading. What can be the alternative for this in Objective-C? Or should I always use different method name?
suse
  • 10,503
  • 23
  • 79
  • 113
134
votes
5 answers

how to "reimport" module to python then code be changed after import

I have a foo.py def foo(): print "test" In IPython I use: In [6]: import foo In [7]: foo.foo() test Then I changed the foo() to: def foo(): print "test changed" In IPython, the result for invoking is still test: In [10]: import foo In…
user478514
  • 3,859
  • 10
  • 33
  • 42
127
votes
11 answers

method overloading vs optional parameter in C# 4.0

which one is better? at a glance optional parameter seems better (less code, less XML documentation, etc), but why do most MSDN library classes use overloading instead of optional parameters? Is there any special thing you have to take note when you…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
123
votes
11 answers

Why is a public const method not called when the non-const one is private?

Consider this code: struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; …
Narek
  • 38,779
  • 79
  • 233
  • 389
121
votes
16 answers

Why can't I overload constructors in PHP?

I have abandoned all hope of ever being able to overload my constructors in PHP, so what I'd really like to know is why. Is there even a reason for it? Does it create inherently bad code? Is it widely accepted language design to not allow it, or are…
tom
  • 2,236
  • 2
  • 18
  • 26
119
votes
5 answers

How does `is_base_of` work?

How does the following code work? typedef char (&yes)[1]; typedef char (&no)[2]; template struct Host { operator B*() const; operator D*(); }; template struct is_base_of { template
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
118
votes
1 answer

Resolving ambiguous overload on function pointer and std::function for a lambda using + (unary plus)

In the following code, the first call to foo is ambiguous, and therefore fails to compile. The second, with the added + before the lambda, resolves to the function pointer overload. #include void foo(std::function f) { f();…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
117
votes
5 answers

Constructor overloading in Java - best practice

There are a few topics similar to this, but I couldn't find one with a sufficient answer. I would like to know what is the best practice for constructor overloading in Java. I already have my own thoughts on the subject, but I'd like to hear more…
Eyal Roth
  • 3,895
  • 6
  • 34
  • 45