【发布时间】:2016-05-01 01:20:33
【问题描述】:
编写非成员函数重载运算符时,第一个参数对应左操作数,第二个参数对应右操作数吗?
我试图重载“
stream << ClassA << ClassB
下面是一个示例,其中FeetInches 是一个具有成员变量feet 和inches 的类。
这就是这个参数顺序起作用的原因吗:
ostream &operator<<(ostream &strm, const FeetInches &obj)
{
strm << obj.feet << " feet, " << obj.inches << " inches";
return strm;
}
--
但是这个参数顺序不起作用?
ostream &operator<<(const FeetInches &obj, ostream &strm)
{
strm << obj.feet << " feet, " << obj.inches << " inches";
return strm;
}
【问题讨论】:
标签: c++ stream operator-overloading