What Does “Overloaded”/“Overload”/“Overloading” Me

2019-01-23 05:03发布

问题:

What does "Overloaded"/"Overload" mean in regards to programming?

回答1:

It means that you are providing a function (method or operator) with the same name, but with a different signature. For example:

void doSomething();
int doSomething(string x);
int doSomething(int a, int b, int c);


回答2:

Basic Concept

Overloading, or "method overloading" is the name of the concept of having more than one methods with the same name but with different parameters.

For e.g. System.DateTime class in c# have more than one ToString method. The standard ToString uses the default culture of the system to convert the datetime to string:

new DateTime(2008, 11, 14).ToString(); // returns "14/11/2008" in America

while another overload of the same method allows the user to customize the format:

new DateTime(2008, 11, 14).ToString("dd MMM yyyy"); // returns "11 Nov 2008"

Sometimes parameter name may be the same but the parameter types may differ:

Convert.ToInt32(123m);

converts a decimal to int while

Convert.ToInt32("123");

converts a string to int.

Overload Resolution

For finding the best overload to call, compiler performs an operation named "overload resolution". For the first example, compiler can find the best method simply by matching the argument count. For the second example, compiler automatically calls the decimal version of replace method if you pass a decimal parameter and calls string version if you pass a string parameter. From the list of possible outputs, if compiler cannot find a suitable one to call, you will get a compiler error like "The best overload does not match the parameters...".

You can find lots of information on how different compilers perform overload resolution.



回答3:

A function is overloaded when it has more than one signature. This means that you can call it with different argument types. For instance, you may have a function for printing a variable on screen, and you can define it for different argument types:

void print(int i);
void print(char i);
void print(UserDefinedType t);

In this case, the function print() would have three overloads.



回答4:

It means having different versions of the same function which take different types of parameters. Such a function is "overloaded". For example, take the following function:

void Print(std::string str) {
  std::cout << str << endl;
}

You can use this function to print a string to the screen. However, this function cannot be used when you want to print an integer, you can then make a second version of the function, like this:

void Print(int i) {
  std::cout << i << endl;
}

Now the function is overloaded, and which version of the function will be called depends on the parameters you give it.



回答5:

Others have answered what an overload is. When you are starting out it gets confused with override/overriding.

As opposed to overloading, overriding is defining a method with the same signature in the subclass (or child class), which overrides the parent classes implementation. Some language require explicit directive, such as virtual member function in C++ or override in Delphi and C#.

using System;

public class DrawingObject
{
    public virtual void Draw()
    {
        Console.WriteLine("I'm just a generic drawing object.");
    }
}

public class Line : DrawingObject
{
    public override void Draw()
    {
        Console.WriteLine("I'm a Line.");
    }
}


回答6:

An overloaded method is one with several options for the number and type of parameters. For instance:

foo(foo)

foo(foo, bar)

both would do relatively the same thing but one has a second parameter for more options

Also you can have the same method take different types

int Convert(int i)
int Convert(double i)
int Convert(float i)


回答7:

Just like in common usage, it refers to something (in this case, a method name), doing more than one job.



回答8:

Overloading is the poor man's version of multimethods from CLOS and other languages. It's the confusing one.

Overriding is the usual OO one. It goes with inheritance, we call it redefinition too (e.g. in https://stackoverflow.com/users/3827/eed3si9n's answer Line provides a specialized definition of Draw().