重载流操作符为什么必须用引用

http://hi.baidu.com/%C8%CE%C5%E0%BB%A8/blog/item/1d3a1cf583a97e34bc310918.html


在C++中,流操作符(>>)是经常要被重载的,一般的形式是这样的:

class CObj
{
    ...
};

std::ostream& operator << (std::ostream& strm, const CObj& objA)
{
    ...
    return strm;
}

我们经常使用流的引用,而不是直接把一个流作为值传送,原因主要考虑到流是一种资源,如果作为值传送,出现了对资源的复制,释放的时候很麻烦,而且可能出现两个流同时操作一个文件的情况。但是这个原因只能说是我们用流作为引用而不是值传送的一个逻辑或性能限制,不是语法限制,也就是说,我们是可以硬把流作为一个值传送过来的。

也就是说,我们实际上可以这样写的:

class CObj
{
    ...
};

std::ostream operator << (std::ostream strm, const CObj& objA)
{
    ...
    return strm;
}

但是这样写了以后,编译器(Visual C++ 7.0)会报告编译时错误,类型不匹配。为什么会这样?难道是语法上有限制吗?

事实上是语法上并没有这样的限制说重载流符号的时候一定要用引用方式来传递流,事实上是,我们在传递参数的任何时候都必须用引用方式传递流,或者指针(这就和引用没区别了),原因在于:流的类体系结构中,不管是哪个流,都只有一个接收指针作为参数而且没有用explicit声明拷贝构造函数,也就是说,所有的流,都没有提供可以用另外一个流作为值传送参数的拷贝构造函数。这是不使用引用传送流引起语法错误的原因。

如果我们使用值传送来传递一个流给函数,那么在函数里要生成一个该流的临时变量,生成临时变量的时候,就要调用对应的拷贝构造函数,并且这个拷贝构造函数必须是以一个值传送的流作为参数的——但是流就是没有这样的拷贝构造函数。

结果,编译时错误就产生了!

 

WHY STREAM ONLY CAN BE PASSED AS REFERENCE WHEN OVERLOAD A STREAM OPERATOR?

In C plus plus, stream opeator (>>) is offen overloaded, and we always overload it like this:


class CObj
{
    ...
};

std::ostream& operator << (std::ostream& strm, const CObj& objA)
{
    ...
    return strm;
}


When we pass stream as a parameter to a function, we will use reference, not value passed,  the main reason is that stream is a kind of resource, if we pass it as a valued passed parameter, the programme will copy the resource, and then the resource which is copied twice may be releaseed twice too, and this, may cause trouble. For the more, copying a stream also can cause two or more streams write to one file at same time. But this is only a reason in the logic, not a grammer rule, that is to say, we can pass stream as valued passed parameter, althought this is wrong:

class CObj
{
    ...
};

std::ostream operator << (std::ostream strm, const CObj& objA)
{
    ...
    return strm;
}

If we really write like this, the compiler (I use Visual C++ 7.0) will report a compile-time error, type mismatch or construcor not found (I can't remember the actual one). Why? Is it a grammer rule?

In fact, it is not a grammer rule that you can not pass stream as value when you overload the stream operator, in fact, it is a grammer rule that we MUST pass stream as reference or pointer (pointer is reference, for the compiler) when we want to pass stream as a parameter to a function. The reason is: In the structure of stream's class, no matter which stream it is, it only have one copy constructor without explicit which can accept a pointer of stream, that is to say, all the streams, do not have a copy constructor which can accept another stream as a value. This is the reason which cause an error if we pass a stream as a value.

If we pass a stream as a value to a function, the function will create a temporer vaiable, the temperor variable is created by a copy constructor which can accept a stream as value, but all the streams do not have such a copy sonsturcutor.

Compile-time error occur as a result! 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值