如何将std :: ofstream重定向到std :: cout? [英] How can I redirect a std::ofstream to std::cout?
问题描述
我正在使用一些代码,这些代码使用类型为std::ofstream*
的全局调试记录器.我想将其重定向到std :: cout,因为我是实时使用代码,而不是为其设计的批处理方法.
I'm working with some code that uses a global debug logger that is of type std::ofstream*
. I would like to redirect this to std::cout since I'm using the code in realtime, as opposed to a batch method for which it was designed.
是否可以将其使用的全局std::ofstream*
指针重定向到std::cout
?我知道std::ofstream
是从std::ios
继承的,它允许人们使用rdbuf()
方法更改流缓冲区,但不幸的是,它似乎std::ofstream
重新定义了rdbuf()
方法,这使得以下代码无法编译:>
Is it possible to redirect the global std::ofstream*
pointer it uses to std::cout
? I know std::ofstream
inherits from std::ios
, which allows one to change the stream buffer using the rdbuf()
method, but unfortunately it appears std::ofstream
redefines the rdbuf()
method, which makes the following code not compile:
gOsTrace = new std::ofstream();
gOsTrace->rdbuf(std::cout.rdbuf());
还有另一种方法可以重定向gOsTrace
对象以指向cout
吗?
Is there another way to redirect the gOsTrace
object to point to cout
?
推荐答案
具体IOStream流类的rdbuf()
方法隐藏了std::ios
中声明的方法.您需要明确的限定条件才能找到基类重载:
The rdbuf()
method of the concrete IOStream stream classes hide the one declared in std::ios
. You will need an explicit qualification in order to find the base class overload:
gOsTrace->basic_ios<char>::rdbuf(std::cout.rdbuf());
这篇关于如何将std :: ofstream重定向到std :: cout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!