为什么"ostream & os"必须在那里C++操作员过载?
Why the "ostream & os" must be there for C++ operator overload?
// The original code is from link: http://hoeven.blogbus.com/logs/37324287.html
#include<iostream>
#include<vector>
#include <stdlib.h>
using namespace std;
class test{
public:
int v;
/*构造函数*/
test():v(0){}
test(const int &a):v(a){}
test(const test &t1):v(t1.v){}
/*以下重载小于号 < */
//比较两个对象的大小
bool operator<(const test &t1) const{
return (v < t1.v);
}
//比较对象和int的大小
bool operator<(const int &t1) const{
return (v < t1);
}
//友元函数,比较int和对象的大小
friend inline bool operator<(const int &a, const test & t1){
return (a < t1.v);
}
/*以下重载赋值号 = */
//对象间赋值
test & operator=(const test &t1){
v = t1.v;
return *this;
}
//int赋值给对象
test & operator=(const int &t1){
v = t1;
return *this;
}
/*以下重载加号 + */
//对象加上 int
test operator+(const int & a){
test t1;
t1.v = v + a;
return t1;
}
//对象加对象
test operator+(test &t1){
test t2;
t2.v = v + t1.v;
return t2;
}
/*以下重载加等号 += */
//对象加上对象
test &operator+=(const test &t1){
v += t1.v;
return *this;
}
//对象加上int
test &operator+=(const int &a){
v += a;
return *this;
}
/*以下重载双等号 == */
//对象==对象
bool operator==(const test &t1)const{
return (v == t1.v);
}
//对象==int
bool operator==(const int &t1)const{
return (v == t1);
}
/*以下重载 输入>> 输出<< */
/*友元函数,输出对象*/
friend inline ostream & operator << (ostream & os, test &t1){
cout << "class t(" << t1.v << ")" << endl;
return os;
}
/*友元函数,输入对象*/
friend inline istream & operator >> (istream & is, test &t1){
cin >> t1.v;
return is;
}
};
int main(){
test t0, t1(3); // t0 has no initial value, so use default value 0
test t2(t1);
cout << t0 << t1 << t2;
cin >> t1;
t2 = t1;
t2 += t1;
t1 += 10;
cout << t2;
if(t1 < t2) cout << "t1 < t2";
else if(t1 == t2) cout << "t1 = t2";
else /* t1 > t2*/ cout << "t1 > t2";
cout <<endl;
system("echo Tom");
return 0;
}
/*
$ ./a.out
class t(0)
class t(3)
class t(3)
45
class t(90)
t1 < t2
Tom
*/
完整的代码如上。但我不明白为什么括号中必须有"ostream & os"(见下文)?如果我删除"ostream & os",就会给出很多错误。
friend inline ostream & operator << (ostream & os, test &t1){
cout << "class t(" << t1.v << ")" << endl;
return os;
}
因为您粘贴的代码是错误的。
<<运算符应该写入其参数,即您的函数应该是
inline ostream & operator << (ostream &os, test &t1) {
os << "class t(" << t1.v << ")" << endl;
return os;
}
这使您能够写入任何流,而不仅仅是cout
.在前面的代码中,如果您编写ofile << t1;
(考虑到ofile
是一个文件流),则不会写入文件,但仍会使用标准输出。
您有两种重载operator<<
的方法:
- 一元 (
this->operator<<(other_object)
), - 二进制 (
operator<<(ostream& os, other_object
)。
其中只有一个是可能的(第二个),第一个是不可能的,因为要实现它,你必须重载ostream类运算符的功能<<这是无法做到的。
当你像cout << "some text";
二进制运算符一样编写语句时,它会调用参数:类ostream
的对象和另一个类
ostream & os
是operator <<
的左侧。 即 cout << myObject;
.在这种情况下,cout
是ostream
。但是,在重载中,您将忽略os
变量并无条件地使用 cout
。您应该在operator <<
重载中将cout
替换为os
。
只有将 ostream
变量传递给方法,该方法才知道输出到哪里。您也可以打开文件流并将其传递给该方法。然后,输出将写入文件而不是标准输出。
ostream 参数为您提供了更大的灵活性,可以确定将输出指向何处。它还允许链接,以便可以在同一语句中输出多个项目:
test t1;
test t2(5);
test t3(-4);
// Write "0 5 -4n" to standard output
cout << t1 << " " << t2 << " " << t3 << endl;
// Write "0 5 -4n" to the file my_file.txt
ofstream ofs("my_file.txt");
ofs << t1 << " " << t2 << " " << t3 << endl;
// Write "0 5 -4n" to oss, which is then written to standard output
ostringstream oss;
oss << t1 << " " << t2 << " " << t3 << endl;
cout << oss.str() << endl;
相关文章:
- <<操作员在下面的行中工作
- C++ 与操作员不匹配<<
- 操作员C++的模棱两可的过载
- C++中>>操作员过载时出现问题?
- NaN 上的宇宙飞船操作员
- 比根<操作员
- SFINAE不能防止模棱两可的操作员过载吗?
- 什么是现实中的"endl"(或任何输出操纵器)?它是如何实现的,它如何与操作员<<一起工
- 为什么"delete"操作员给我访问权限冲突
- 为什么使操作员成为新的专用会打破 std::shared_ptr?
- 在这种情况下是私有的吗?试图使操作员<<过载
- C++操作员过载>>
- 是否有 C++20 浮点数的包装器,使我能够默认宇宙飞船操作员?
- 与异常处理程序中的操作员<<不匹配
- 复印作业操作员说明
- 友谊和操作员+=重载
- >操作员在比较两个C++容器时会怎么做?
- 与操作员比较两个计时时间点
- C++操作员订单评估
- 如何强制新操作员根据我的需要解释语句