简体   繁体   中英

What is the difference between polymorphism and overloading?

我理解多态性并且模糊地理解重载,但是会理解那些完全理解有两个概念的人来解释分类差异是什么以及重载是否是多态的形式(似乎对此有不同意见)。

Polymorphism, at its core, is about multiple things which all have a certain set of consistent behavior, such that you can replace one with another within a particular algorithm or process. So long as they all provide the expected interface, the process still works.

Overloading doesn't really have such a foundation. It is merely the ability to name two or more functions with the same name, so long as they have different parameter lists. The compiler figure out which function you actually meant based on the types of the arguments you pass.

Now overloading can be used to create polymorphism. Consider the following:

template<typename T>
void func(T t) {call(t);}

This will call call , passing t as a parameter. This will work so long as you provide a type T for which call(t) is legitimate C++ code. You could do this by overloading the function call for whatever types you are interested in using with func :

void call(int);
void call(float);
void call(vector<int>);

In this way, func is a function which is polymorphic (statically) with respect to its parameter. It can perform its operation on any type, so long as that type has the appropriate interface. That interface being the ability to call a function call with a variable of that type.

func(5); //Legal
func(13.4); //Legal
func(vector<int>{4, 3, 2, 1}); //Legal
func(vector<float>{}); //NOT legal

Here, we use function overloading of call to create a form of polymophism through the func function. But this does not mean that overloading is polymorphism.

Overloading is a language tool. Polymorphism is a concept. Polymorphism is about making multiple objects all work the same way. Overloading is just a way to give different functions the same name.

Polymorphism: one method which has multiple implementations depending on the type of the argument(s) with which it is invoked. Often not known at compile time. The number of arguments is fixed, and in C++ the only argument whose type matters is the first one ( this ). In C++ the common version of this requires a base class with virtual methods.

Overloading: one name which is actually multiple methods depending on the count and type of the argument(s) with which it is invoked. Always decided at compile time. No base class is involved.

If you'd like an analogy: polymorphism is when you hire a dozen specialist mechanics to work on cars, and each one of them has functions like do_work(vehicle) and take_vacation(duration) . Each one does something different but they all have the same signature apart from the "implicit this" argument in C++ (aka self in Python, etc.). Overloading is when you hire generalist mechanics and each one of them has do_work(steering) , do_work(lighting) , do_work(engine) , etc.

Overloading is a subset of Polymorphism. Polymorphism is of two types: On run-time and compile-time. Overloading belongs to the latter one ( there are some other facets of compile-time/static polymorphism as well like templates , function pointers )

Usually authors refer Polymorphism to the one done on the Run-time ( unless specified ) and if you are talking about that Polymorphism, then clearly that one is done on runtime ( John's answer already drew the basic differences wrt inheritance/virtual function in his answer there ) and overloading is done on compile time when your function parameters determine which overload to be called.

Overloading - Static Polymorphism (Demonstration)

Following screenshot can demonstrate how function overloading is static ( resolved before run time )

在此输入图像描述

PS

In case, you would like to see, this is a related question about overloading and virtual functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM