为什么 C++ 中要添加函数重载?

发布于 2024-10-05 08:19:37 字数 86 浏览 3 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

两相知 2024-10-12 08:19:37

它提高了可维护性。如果你有一个类型 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.

随梦而飞# 2024-10-12 08:19:37

除了 DeadMG 所说的之外,还有一个很好的理由是,如果您正在编写一个模板函数,例如调用 sqrt,那么您需要一种通用调用的方法>sqrt - 如果您必须尝试以某种方式将名称更改为 sqrtfsqrtd 等(具体取决于类型),这将非常困难模板参数。重载解决了这个问题,因为你只需编写 sqrt 并让编译器确定它应该使用哪个重载:

template <typename T>
T sqrt_plus_one(T t) // contrived example
{
  return sqrt(t) + 1;
}

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 calling sqrt -- it would be very difficult if you had to try and somehow vary the name to sqrtf, sqrtd, etc., depending on the type of the template parameter. Overloading solves this problem, because then you just write sqrt and let the compiler figure out which overload it should be using:

template <typename T>
T sqrt_plus_one(T t) // contrived example
{
  return sqrt(t) + 1;
}
べ繥欢鉨o。 2024-10-12 08:19:37

您更喜欢在 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.

溺渁∝ 2024-10-12 08:19:37

你可以直接从马口中得到答案:The Design and Evolution of C++ by Bjarne Stroustrup整整一章介绍了重载、它的历史、演变、设计权衡和决策。

我不会在这里重述这个故事,但会提到一些有趣的历史事实:

  • 运算符和函数重载密切相关;
  • 在早期的 C++ 中,曾经有一个特殊的关键字(overload)必须用于将标识符声明为重载;
  • 函数重载需要类型安全链接(即名称修改);首次实现时,它帮助发现了现有 C 和 C++ 代码中数量惊人的链接时错误(引用 Stroustrup 的话,这就像“第一次在 C 程序上运行 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:

  • operator and function overloading are closely related;
  • in early C++ there used to be a special keyword (overload) that had to be used to declare an identifier as overloaded;
  • function overloading requires type-safe linking (i.e. name mangling); when first implemented, it helped discover a surprising number of link-time errors in existing C and C++ code (to quote Stroustrup, it was like "running lint on a C program for the first time -- somewhat embarrassing".)
≈。彩虹 2024-10-12 08:19:37

如果不是为了函数重载,请尝试想出一种舒适的方法来构造对象。

std::string foo = "bar";
std::vector< std::string > myStringVector;
myStringVector.push_back( std::string() );
myStringVector.push_back( std::string( "hello" ) );
myStringVector.push_back( std::string( foo ) );

当然,这是一个无意义的例子,但它说明了这一点。

另一点是模板编程。如果您必须为每个参数类型使用不同的函数名称,您就无法想出通用模板。

Try to come up with a comfortable way to construct objects if it weren't for function overloading.

std::string foo = "bar";
std::vector< std::string > myStringVector;
myStringVector.push_back( std::string() );
myStringVector.push_back( std::string( "hello" ) );
myStringVector.push_back( std::string( foo ) );

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.

菊凝晚露 2024-10-12 08:19:37

使用多态性,我们可以设计一系列具有相同函数名但具有不同参数列表的函数。该函数将根据函数调用中的参数列表执行不同的操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文