C ++重载输出运算符 [英] C++ overloaded output operator
问题描述
所以我编写我的c ++家庭作业分配,并有一个最后部分,他希望我们用一个重载的输出/插入操作符替换格式化的输出方法(toString)。要100%诚实我不知道他是什么意思。我已经搜索了一下,发现使用重载插入操作符的示例代码,但似乎不能找到如何将它合并到我的代码。虽然我认为我可能在错误的地方看。我的toString如下:
so I am coding my c++ homework assignment and there is a final part where he wants us to Replace the formatted output method (toString) with an overloaded output/insertion operator. TO be 100% honest I have no idea what he means by this. I've searched around a bit and found example codes using an overloaded insertion operator, but can't seem to find how to incorporate it into my code. Though I think I may be looking in the wrong place. My toString is as follows:
string Movie::toString() const {
ostringstream oS;
oS << "\n\n====================== Movie Information\n"
<< "\n Movie Title:\t" << title << " (" << releaseYear << ")"
<< "\n US Rank & Box Office:\t" << usRank << "\t$" << usBoxOffice
<< "\nNon-US Rank & Box Office:\t" << nonUSRank << "\t$" << nonUSBoxOffice
<< "\n World Rank & Box Office:\t" << worldRank << "\t$" << worldBoxOffice
<< "\n";
return oS.str();
}
像我提到的,我不知道什么是overloaded由于某些原因,这不是足够的信息,您可以直接帮助我的问题,那么你可以给我一个简短的描述,他可能是什么意思,用一个重载的输出运算符替换当前的输出。谢谢
Like I mentioned I'm not sure what "overloaded" means, so If for some reason this isn't enough information for you to help me with the problem directly, then can you give me a brief description of what he may mean by replacing the current output with an overloaded output operator. Thank You
编辑:这是我的下一个问题。 C ++重载的输出运算符Cont
edit: This is the next question I have. C++ overloaded output operator Cont
推荐答案
重载函数意味着提供具有相同名称但不同参数类型的其他函数。操作员也可以重载。许多运算符具有可以被重载的相应函数,称为运算符
,其中Σ
是运算符本身。例如,如果您有两个类 T $ c>的
x
和 y
$ c>,可以重载 operator +
。重载操作符允许您为使用该类型的操作符提供一些意义。所以现在你可以做 x + y
。
To overload a function means to provide other functions with the same name but different parameter types. Operators can also be overloaded. Many operators have a corresponding function that can be overloaded called operator??
, where ??
is the operator itself. For example if you have two objects x
and y
of class type T
, you could overload operator+
. Overloading an operator allows you to give some meaning to using that operator with the type. So now you could do x + y
.
流插入操作符 <
。这是你在使用 std :: cin<< hello;
- 它插入到流中。此运算符也可以重载,就像 +
上面的重载。您需要重载的函数称为 operator
The stream insertion operator is <<
. It's what you use when you do std::cin << "hello";
- it inserts into the stream. This operator can also be overloaded, just as +
was overloaded above. The function you need to overload is called operator<<
.
有两种方法来重载二进制运算符如<<
(二进制,因为它需要两个操作数,一个在左侧,一个在右侧, left< / code>)。一个是使它成为
left
类型的成员,并给它一个 right
类型的单个参数。另一个是使它成为具有两个参数的非成员函数,一个类型为 left
,另一个类型为 right
。由于您的左
的类型将是 std :: ostream
,您不能修改类(因为它提供
There are two ways to overload a binary operator like <<
(binary because it takes two operands, one on the left side and one on the right, left << right
). One is to make it a member of the type of left
and give it a single parameter of the type of right
. The other is to make it a non-member function with two parameters, one the type of left
and the other the type of right
. Since the type of your left
will be std::ostream
, you can't modify the class (because it's provided by the standard), so you'll have to go with option two.
所以你的自由功能需要看起来像这样:
So your free function needs to look something like this:
std::ostream& operator<<(std::ostream& os, const Movie& movie) {
// Insert everything you want into `os`
return os;
}
现在,当您执行 ; <$ / code>在左侧有一个
std :: ostream
,在上有一个
Movie
这篇关于C ++重载输出运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!