What is the use/advantage of function overloading?

2019-01-17 15:29发布

What is the use/advantage of function overloading?

10条回答
不美不萌又怎样
2楼-- · 2019-01-17 15:52

It is the mechanism of function with same name but perform different task on different instance. We easily remember the names that are commonly used.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-17 15:55

You might want to do similar things in code with different parameters. If you had to give every function a different name, code readability would be very bad.

查看更多
干净又极端
4楼-- · 2019-01-17 16:00

IMO, the primary benefit is consistency in the naming of methods / functions which logically perform very similar tasks, and differ slightly in by accepting different parameters. This allows the same method name to be reused across multiple implementations.

e.g. The overloads: (Good)

function Person[] FindPersons(string nameOfPerson) { ... }
function Person[] FindPersons(date dateOfBirth) { ... }
function Person[] FindPersons(int age, string dogsName) { ... }

Are preferable to 'uniquely named' functions: (Worse)

function Person[] FindPersonsByName(string nameOfPerson) { ... }
function Person[] FindPersonsByDOB(date dateOfBirth) { ... }
function Person[] FindPersonsByAgeAndDogsName(int age, string dogsName) { ... }

This way the coder writing a client which calls / consumes these functions can operate at a higher level of conceptual thinking ("I need to find a person") and doesn't need to remember / locate a contrived function name.

With static typing, the compiler will be left to match the applicable overload based on the usage parameters. For dynamic typing, this same match up will happen at run time, possibly resulting in failure if no appropriate match is found.

查看更多
叼着烟拽天下
5楼-- · 2019-01-17 16:05

Overloading is a form of polymorphism. It allows the programmer to write functions to do conceptually the same thing on different types of data without changing the name. (It also allows the programmer to write functions to do conceptually different things depending on parameters, but that's a Real Bad Idea.)

This allows consistency in notation, which is good both for reading and for writing code. I/O is a very common use. In most languages in common use, there's a function or operator that will output whatever you like, such as printf() and kin in C, operator<<() in C++, PRINT in the old BASICS I used to use, whatever. Languages that require functions like printint(), printstring(), printfloat(), and the like have never caught on.

It works very well with C++ templates and any other construct where you don't necessarily know what the variable type is at the time of writing the code.

查看更多
【Aperson】
6楼-- · 2019-01-17 16:06
  1. Multiple behaviors to the same function based on the parameters.
  2. Your function may want to work with some optional details. For example, the following example wants to add a member to the Members object, with whatever detail the user knows. Here age is the minimum detail to create a member, age and memberOf are optional. [Note: definition of functions are not provided in the code snippet.]

    public class Members
    {
        public System.Collections.Generic.List<Member> TeamMembers;
    
        public AddMember(Member m) {}
        public AddMember(string name) {}
        public AddMember(string name, int age) {}
        public AddMember(string name, int age, string[] memberOf) {}
    
        public class Member
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string[] MemberOf { get; set; }
        }
    }
    
  3. You may want your method to be suitable for multiple type of objects. ex. Console.WriteLine() method is capable of writing empty line, bool, int, string, char[], float etc. on the console. This was made possible because of function overloading.

查看更多
该账号已被封号
7楼-- · 2019-01-17 16:08

Sometimes you have multiple ways of accomplishing the same thing based on the context and inputs available. For type-strict static languages the function definitions can be pretty rigid and need to be explicitly defined ahead of time.

Constructors are generally the best classic example of this. If you're building a complex object and don't have all the pieces, you still want to be able to pass what you have to a constructor and let it fill in the rest. And what you have may vary wildly and need to be defined in different ways as parameters to the constructors.

查看更多
登录 后发表回答