C++ >> 和 << 运算符重载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4066666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
>> and << operator overloading
提问by Earlz
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.
重载运算符的函数的返回类型
<<
必须是对 ostream 对象的引用。
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..
这对我来说似乎完全不对。当然,C++ 比这更开放。但我想无论如何我都会在这里问。这是如何正确(或错误)?当涉及到运算符重载时,我的 C++ 知识开始真正消失。
回答by Thanatos
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:
C++ 不要求返回类型是对ostream
对象的引用。但是,如果您尝试执行以下操作:
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.
为了进一步扩展这一点,<<
运算符的最初用途是用于位移位。1 << 8
例如,是 256。C++ 为此添加了一个(有点令人困惑)第二次使用,并将其重载ostream
以表示流的“输出”。你可以在重载运算符中做任何你喜欢的事情——它就像一个函数一样工作,但是,运算符有一个人类的期望:程序员期望,在 C++ 中,这<<
是位移或流输出。
回答by Alok Save
The return type of the function to overload the operator
<<
must be a reference to anostream
object.
重载运算符的函数的返回类型
<<
必须是对ostream
对象的引用。
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
.
说“必须”是不正确的,“通常”可能是正确的词,为什么?因为正如大多数答案已经指出的那样,它object chaining
在使用iostreams
.
回答by Chubsdad
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;
回答by André Caron
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=
.
从更一般的角度来看,operator<<
应该始终返回它的左侧操作数以链接调用,就像operator=
.
When dealing with the <iostreams>
library, this happens to be a reference to std::ostream
.
在处理<iostreams>
库时,这恰好是对std::ostream
.
回答by user207421
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.
这是不对的。它只在 iostreams 的上下文中是正确的,在我可能不相关和无趣的观点中,它不应该以这种形式被放出笼子。如果您的代码中不包含 iostreams,您可以随心所欲。但是我不会重载这些运算符来做任何事情,除了移位类,无论这意味着什么,通过整数值,或者通过可以以某种方式减少到整数值的类。
回答by Ben Hymanson
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
让它返回 ostream 引用的目的是让您可以将它们链接在一起。否则你必须写cout << 1; cout << " is a number"; cout << endl