为什么 ostream::operator<< char 参数的全局函数?

发布于 2024-10-15 10:10:08 字数 265 浏览 7 评论 0 原文

根据 http://www.cplusplus.com/reference/iostream/ ostream/operator%3C%3C/ 运算符<<例如,streambuf 上定义的方法是 ostream 的成员,但对于 char / char * 来说,它是一个全局函数。这种差异背后的设计决策是什么?

Acording to http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/ the operator<< method defined on e.g. streambuf is a member of ostream, but for char / char * it is a global function. What's the design decision behind this difference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浅浅淡淡 2024-10-22 10:10:08

streambuf*operator<< (或 int 听起来更简单)和 char 可能都是作为会员运营商或非会员(免费)运营商实施。

我的猜测是,这是由于在定义 C++ 时出现了向后兼容性问题:也许较旧的代码正在依赖成员 operator<<(int),因此他们决定不将其移动为一个免费的运营商。

C++ 标准库(还有 STL)有许多像这样的不均匀性。

operator<< for streambuf* (or int which sounds a simpler case) and char could have been both implemented as member operators, or as non-member (free) operators.

My guess is that it's due to retro-compatibility issues emerged while C++ was being defined: maybe older code was relaying on a member operator<<(int), and thus they decided not to move it as a free operator.

The C++ standard library (and also STL) have a number of dishomogeneity like this one.

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