What Does “Overloaded”/“Overload”/“Overloading” Mean?

前端 未结 8 1203
清歌不尽
清歌不尽 2020-12-09 10:33

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

相关标签:
8条回答
  • 2020-12-09 10:55

    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);
    
    0 讨论(0)
  • 2020-12-09 11:04

    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.

    0 讨论(0)
  • 2020-12-09 11:05

    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.

    0 讨论(0)
  • 2020-12-09 11:05

    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.");
        }
    }
    
    0 讨论(0)
  • 2020-12-09 11:11

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

    0 讨论(0)
  • 2020-12-09 11:14

    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)
    
    0 讨论(0)
提交回复
热议问题