运算符重载疑惑
Operator overload doubts
我试图编写一些运算符重载函数,尤其是 <<
运算符以将其与自定义 class 和 std::ofstream
对象一起使用,但我有点困惑在网上找到的各种示例中使用的语法。例如,让我们将 operator<<
重载视为简单自定义 class:
的非成员函数
#include <fstream>
class Example {
public:
int first;
int second;
};
// I found this kind of operator on the Internet
std::ofstream& operator<< (std::ofstream& out, Example obj) {
out << obj.first << endl << obj.second;
return out;
}
int main() {
Example a={1,2};
std::ofstream out;
out.open("test");
out << a;
out.close();
}
我真的不明白为什么它应该 return std::ofstream&
才能正常工作。我尝试使用以下运算符
void operator<< (std::ofstream& out, Example obj) {
out << obj.first << endl << obj.second << endl;
}
而且效果也不错。我的意思是, out << obj;
不能解释为 operator<< (out , obj);
吗?为什么它必须 return 因为我正在传递对 std::ofstream
对象的引用?
当我尝试编写一个 operator=
重载作为自定义 class 的成员函数时,出现了同样的疑问,如下面的简单示例
class Custom{
public:
int up;
int down;
Custom& operator= (Custom a) {
up=a.up;
down=a.down;
return *this;
}
};
我对赋值运算符使用了复制交换惯用语,所以不要太在意运算符定义,这只是一个例子。再次,写
Custom obj1, obj2;
obj1 = obj2;
既然我可以将 obj1 = obj2;
解释为 obj1.operator=(obj2)
,为什么需要 return 类型 Custom&
而不是 void
?
如果您希望能够将 operator<<
链接在一起,您必须使用 return 类型(std::ostream&
优于 std::ofstream&
,因此您可以使用它也是 std::cout
和喜欢的)。
out << a << b;
(out << a) << b;
^^^^^^^^^^
lhs has to be a stream
对于赋值运算符,道理本质上是一样的。 C++ 语法允许您编写许多需要 return 类型的表达式,例如:
Custom obj1, obj2, obj3;
(obj1 = obj2) + obj3 ... // assign obj2 to obj1 and work with that...
返回引用允许您链接运算符,例如
std::cout << e1 << e2;
Return 一个 reference 而不是 void
,这样就可以写成
out << obj1 << obj2 << obj3;
对于operator=
,你可以写
obj1=obj2=obj3;
您可以编写类似 cout << "First operand" << "Second operand"
的内容,因为第一个操作数 returns 引用 ostream
并且第二个操作数使用此引用。
operator=
以同样的方式工作。可以写a = b = c
,也可以放在if (a = b)
或while (a = b)
里面。这可以使您的代码更短,但有点危险。
我试图编写一些运算符重载函数,尤其是 <<
运算符以将其与自定义 class 和 std::ofstream
对象一起使用,但我有点困惑在网上找到的各种示例中使用的语法。例如,让我们将 operator<<
重载视为简单自定义 class:
#include <fstream>
class Example {
public:
int first;
int second;
};
// I found this kind of operator on the Internet
std::ofstream& operator<< (std::ofstream& out, Example obj) {
out << obj.first << endl << obj.second;
return out;
}
int main() {
Example a={1,2};
std::ofstream out;
out.open("test");
out << a;
out.close();
}
我真的不明白为什么它应该 return std::ofstream&
才能正常工作。我尝试使用以下运算符
void operator<< (std::ofstream& out, Example obj) {
out << obj.first << endl << obj.second << endl;
}
而且效果也不错。我的意思是, out << obj;
不能解释为 operator<< (out , obj);
吗?为什么它必须 return 因为我正在传递对 std::ofstream
对象的引用?
当我尝试编写一个 operator=
重载作为自定义 class 的成员函数时,出现了同样的疑问,如下面的简单示例
class Custom{
public:
int up;
int down;
Custom& operator= (Custom a) {
up=a.up;
down=a.down;
return *this;
}
};
我对赋值运算符使用了复制交换惯用语,所以不要太在意运算符定义,这只是一个例子。再次,写
Custom obj1, obj2;
obj1 = obj2;
既然我可以将 obj1 = obj2;
解释为 obj1.operator=(obj2)
,为什么需要 return 类型 Custom&
而不是 void
?
如果您希望能够将 operator<<
链接在一起,您必须使用 return 类型(std::ostream&
优于 std::ofstream&
,因此您可以使用它也是 std::cout
和喜欢的)。
out << a << b;
(out << a) << b;
^^^^^^^^^^
lhs has to be a stream
对于赋值运算符,道理本质上是一样的。 C++ 语法允许您编写许多需要 return 类型的表达式,例如:
Custom obj1, obj2, obj3;
(obj1 = obj2) + obj3 ... // assign obj2 to obj1 and work with that...
返回引用允许您链接运算符,例如
std::cout << e1 << e2;
Return 一个 reference 而不是 void
,这样就可以写成
out << obj1 << obj2 << obj3;
对于operator=
,你可以写
obj1=obj2=obj3;
您可以编写类似 cout << "First operand" << "Second operand"
的内容,因为第一个操作数 returns 引用 ostream
并且第二个操作数使用此引用。
operator=
以同样的方式工作。可以写a = b = c
,也可以放在if (a = b)
或while (a = b)
里面。这可以使您的代码更短,但有点危险。