为什么 C++ 中要添加函数重载?
我有C背景。我只是想知道为什么 C++ 中要添加函数重载? C 没有函数重载,但 C++ 有,为什么需要它呢?
当时的语言设计者的想法是什么?
I have a C background. I was just wondering why was function overloading added to C++? C doesn't have function overloading but C++ does, what was the need for it?
What went across the mind of the language designer at that time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它提高了可维护性。如果你有一个类型 T 并且用它调用一个函数,那么你需要更改 T,如果该函数已为新 T 重载,那么你可以立即重新编译。在 C 中,您必须返回并挖掘所有调用站点并更改所调用的函数。取开方()。如果你想 sqrt() 一个浮点数,那么你必须改为 sqrtf()。
不仅如此,C++ 类型系统的数量和复杂性远远超过 C,并且必须为每个可能的重载使用单独的函数名称,这将很快耗尽服务于相同目的但采用不同参数的函数的合理名称池。 ,因为现在有更多的论点需要考虑。
例如,比较 C 和 C++ 字符串库。 C 字符串库提供了一种附加到字符串的方法 - strcat()。 C++ 的 std::string::append 有八个重载。你想怎么称呼他们?追加a、追加b等?这太荒谬了——它们都具有相同的功能,只是以不同的方式。
编辑:实际上值得一提的是,append 是一个非常糟糕的例子,许多 C++ 字符串重载都是非常多余的。然而,这是更一般的情况,并不是所有这些重载都是多余的。
It increases maintainability. If you have a type T and you call a function with it, then you need to change T, if the function has been overloaded for the new T then you can recompile instantly. In C you would have to go back and dig through all the call sites and change the function called. Take sqrt(). If you want to sqrt() a float, then you have to change to sqrtf().
Not just that, but the volume and complexity of C++'s type system is far more than in C, and having to have separate function names for every possible overload would quickly exhaust the reasonable pool of names for functions that serve the same purpose but take different arguments, because now there's a lot more arguments to take.
For example, compare the C and C++ string libraries. The C string library offers one method to append to a string - strcat(). C++'s std::string::append has eight overloads. What do you want to call them? append_a, append_b, etc? That's ridiculous- they all serve the same function, just in different ways.
Edit: It is actually worth mentioning that
append
is a really bad example, many of the C++ string overloads are very redundant. However, this is more general case than that and not all of those overloads redundant.除了 DeadMG 所说的之外,还有一个很好的理由是,如果您正在编写一个模板函数,例如调用
sqrt
,那么您需要一种通用调用的方法>sqrt
- 如果您必须尝试以某种方式将名称更改为sqrtf
、sqrtd
等(具体取决于类型),这将非常困难模板参数。重载解决了这个问题,因为你只需编写sqrt
并让编译器确定它应该使用哪个重载:One good reason, in addition to what DeadMG said, is that if you're writing a template function which e.g. calls
sqrt
, then you need a generic way of callingsqrt
-- it would be very difficult if you had to try and somehow vary the name tosqrtf
,sqrtd
, etc., depending on the type of the template parameter. Overloading solves this problem, because then you just writesqrt
and let the compiler figure out which overload it should be using:您更喜欢在 abs/labs/llabs/fabs/fabsf/fabsl 中“选择”一个还是只是 abs() ?
显然,abs()。
因此,除了其他优点之外,大多数时候,函数重载对程序员来说是一种解脱。
Would you prefer "selecting" one among abs/labs/llabs/fabs/fabsf/fabsl Or just abs()?
Obviously, abs().
So function overloading is a relief for programmers, most of the time, beside other advantages.
你可以直接从马口中得到答案:The Design and Evolution of C++ by Bjarne Stroustrup整整一章介绍了重载、它的历史、演变、设计权衡和决策。
我不会在这里重述这个故事,但会提到一些有趣的历史事实:
overload
)必须用于将标识符声明为重载;lint
- 有点像”)令人尴尬”。)You could get the answer straight from the horse's mouth: The Design and Evolution of C++ by Bjarne Stroustrup devotes an entire chapter to overloading, its history, evolution, design tradeoffs and decisions.
I won't recount the story here, but will mention a couple of interesting historical facts:
overload
) that had to be used to declare an identifier as overloaded;lint
on a C program for the first time -- somewhat embarrassing".)如果不是为了函数重载,请尝试想出一种舒适的方法来构造对象。
当然,这是一个无意义的例子,但它说明了这一点。
另一点是模板编程。如果您必须为每个参数类型使用不同的函数名称,您就无法想出通用模板。
Try to come up with a comfortable way to construct objects if it weren't for function overloading.
A nonsense example, of course, but it illustrates the point.
Another point would be template programming. You could not come up with generic templates if you had to have a different function name for each parameter type.
使用多态性,我们可以设计一系列具有相同函数名但具有不同参数列表的函数。该函数将根据函数调用中的参数列表执行不同的操作。
Using polymorphism , we can design a family of functions with same function name but with different argument list . The function would perform different operations depending on the arguments list in the function call.