>>>和<<运算符重载

发布于 2024-09-30 05:19:16 字数 215 浏览 6 评论 0 原文

我刚刚为我的编程课做了一个测验,并答错了这个问题:

函数的返回类型 重载运算符 << 必须是 对 ostream 对象的引用。

这对我来说似乎根本不对。当然,C++ 比这更开放。但我想我还是会在这里问。这如何是对的(或错的)?当涉及到运算符重载时,我的 C++ 知识开始真正消失。

I just did a quiz for my programming class and got this question wrong:

The return type of the function to
overload the operator << must be a
reference to an ostream object.

This does not seem right at all to me. Surely C++ is a bit more open ended than this. But I thought I'd ask here anyway. How is this right (or wrong)? My C++ knowledge begins to really fade when it comes to operator overloading..

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

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

发布评论

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

评论(6

埖埖迣鎅 2024-10-07 05:19:16

C++ 不要求返回类型是对 ostream 对象的引用。但是,如果您尝试执行以下操作:

cout << instance_of_custom_type << 3 << "hi" << endl;

那么您将需要:

ostream &operator << (ostream &os, custom_type &t);

但是,如果您正在执行诸如编写大整数类型之类的操作,并且想要支持位移位,则可能会类似于:

BigInt operator << (const BigInt &i, unsigned int shift);

为了进一步扩展这一点, << 运算符最初的用途是进行位移位。 <代码>1 <<例如,8 是 256。 C++ 为此添加了第二个用途(有点令人困惑),并在 ostream 上重载它以表示“输出”到流。您可以在重载运算符中做任何您喜欢的事情 - 它的工作方式就像函数一样,但是,运算符具有人类的期望:在 C++ 中,程序员期望 << 是位移位或流输出。

It is not required by C++ that the return type be a reference to an ostream object. However, if you are trying to do something like:

cout << instance_of_custom_type << 3 << "hi" << endl;

Then you will need:

ostream &operator << (ostream &os, custom_type &t);

However, if you were doing something like writing a large integer type, and wanted to support bit shifting, it might be something like:

BigInt operator << (const BigInt &i, unsigned int shift);

To expand this a bit further, the original use of the << operator is for bit shifting. 1 << 8 is 256, for example. C++ added a (slightly confusing) second use for this, and overloaded it on ostream to mean "output" to the stream. You can do whatever you like within an overloaded operator - it works just like a function, however, operators have a human expectation attached with them: programmers expect, in C++, that << is bit shifting or stream output.

温柔戏命师 2024-10-07 05:19:16

将返回类型作为对作为引用参数传递给重载插入运算符的同一流对象的引用,使我们能够编写如下代码

mystream &operator << (mystream &os, myclass &myobject){
   // do whatever
   return os;
}

mystream << myobject << fundamental_type_object;

Having the return type as a refernce to the same stream object passed as reference argument to the overloaded insertion operator enables us to write code such as

mystream &operator << (mystream &os, myclass &myobject){
   // do whatever
   return os;
}

mystream << myobject << fundamental_type_object;
吾性傲以野 2024-10-07 05:19:16

重载运算符<<的函数的返回类型必须是对ostream对象的引用。

说“必须”是不正确的,可能“通常”才是正确的词,为什么?因为正如大多数答案已经指出的那样,它在使用 iostreams 时提供了对象链接的便利。

The return type of the function to overload the operator << must be a reference to an ostream object.

To say 'must' is incorrect, probably 'usually' is the correct word, and why? Because as most of the answers have already pointed out, it gives the convenience of object chaining, while working with iostreams.

小草泠泠 2024-10-07 05:19:16

从更一般的角度来看,operator<< 应始终返回其左侧操作数,以便链式调用,就像 operator= 一样。

在处理 库时,这恰好是对 std::ostream 的引用。

From the more general point of view, operator<< should always return it's left hand side operand in order to chain calls, just like operator=.

When dealing with the <iostreams> library, this happens to be a reference to std::ostream.

囚你心 2024-10-07 05:19:16

让它返回 ostream 引用的目的是为了让您可以将它们链接在一起。否则你必须写 cout << 1;计算<< “是一个数字”;计算<<结束

The purpose of having it return the ostream reference is so that you can chain them together. Otherwise you'd have to write cout << 1; cout << " is a number"; cout << endl

新一帅帅 2024-10-07 05:19:16

这是不对的。它只有在 iostreams 的上下文中才是正确的,在我看来,iostreams 可能是无关紧要且无趣的,它永远不应该以这种形式被放出笼子。如果您的代码中不包含 iostreams,您可以做您喜欢的事情。但我不会重载这些运算符来执行除移位类之外的任何操作,无论这意味着通过整数值,或者通过可以以某种方式简化为整数值的类。

It isn't right. It's only correct in the context of iostreams, which in my probably irrelevant and uninteresting opinion should never have been let out of the cage in that form. If you don't include iostreams in your code you can do what you like. But I wouldn't be overloading these operators to do anything except shift classes, whatever that means, by integer values, or maybe by classes that can be reduced to integer values somehow.

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