【发布时间】:2014-07-29 15:25:12
【问题描述】:
我已经覆盖了std::ostream::flush() 函数。下面我从示例中删除了所有其他代码:
#include <iostream>
#include <ostream>
class CMyStream : public std::streambuf, public std::ostream
{
public:
explicit CMyStream() throw() : std::ostream(this)
{
// Intentionally empty block
}
std::ostream &flush()
{
std::cout << "Overridden flush called\n";
return (*this);
}
int sync()
{
std::cout << "Overridden sync called\n";
return 0; // Success
}
};
我尝试这样使用它:
CMyStream myStream;
myStream << "Test" << std::flush;
,但 std::flush 操纵器不会调用覆盖的 CMyStream::flush() 或 CMyStream::sync() 函数。如果我调试,我会看到默认的 std::ostream::flush() 被调用,而不是我的覆盖函数。
有没有办法解决这个问题,还是我必须直接调用myStream.flush()而不是使用机械手?
【问题讨论】:
标签: c++ templates stl flush ostream