[英]how to temporarily use std::cout in place of std::ofstream
if I want to create a logging class, say如果我想创建一个日志记录 class,比如说
class logging_class {
public:
std::ofstream error;
std::ofstream event;
logging_class() {}
logging_class(string err, string evt) {
error.open(err);
event.open(evt);
}
~logging_class() {
error.close();
event.close();
}
};
so that later I can easily create program log:以便以后我可以轻松创建程序日志:
logging_class logger("Log_Error.txt","Log_Event.txt");
logger.error << "something wrong";
logger.event << "something interesting";
Now, at the early stage of development, I want to redirect all the output to screen (ie std::cout).现在,在开发的早期,我想将所有 output 重定向到屏幕(即 std::cout)。 I don't want to change the usage of logger.error<<"something wrong";
我不想改变logger.error<<"something wrong";
的用法to std::cout<<"something wrong";
到std::cout<<"something wrong";
, because later I will need to change all instances of std::cout
to either logger.error
or logger.event
(maybe thousands of them). ,因为稍后我需要将std::cout
的所有实例更改为logger.error
或logger.event
(可能有数千个)。 Can I make some easy changes in the class logger to do this?我可以在 class 记录器中进行一些简单的更改来做到这一点吗?
Update: Following Enlico's instruction, I can modify the logging_class
as:更新:按照 Enlico 的指示,我可以将logging_class
修改为:
class logging_class {
public:
//std::ofstream error;
//std::ofstream event;
std::ostream& error;
std::ostream& event;
logging_class():error(std::cout),event(std::cout) {}
//logging_class(string err, string evt) {
// error.open(err);
// event.open(evt);
//}
//~logging_class() {
// error.close();
// event.close();
//}
};
and after creating object with default constructor: logging_class logger;
在使用默认构造函数创建 object 之后: logging_class logger;
I can now redirect all the logging to screen display.我现在可以将所有日志记录重定向到屏幕显示。
Think of std::cout
of what it actually is: an object.想想std::cout
实际上是什么:一个 object。 It is just that:就是这样:
The global objects
std::cout
andstd::wcout
control output to […]全局对象std::cout
和std::wcout
控制 output 到 […]
So when you say that you want to temporarily use std::cout
in place of std::ofstream
you're mixing apples (object std::cout
) with oranges (class std::ofstream
).因此,当您说要临时使用std::cout
代替std::ofstream
时,您是在将苹果(对象std::cout
)与橙子(类std::ofstream
)混合在一起。
What you want to do, instead, is to use std::cout
instead of an object of class std::ofstream
.相反,您想要做的是使用std::cout
而不是object 的class std::ofstream
。
But std::cout
is not of class std::ofstream
, so error
can't hold it;但是std::cout
不是 class std::ofstream
,所以error
不能保留它; it is of class std::ostream
, which is a superclass of the former.它属于 class std::ostream
,它是前者的超类。
Therefore, as suggested in a comment, you can make error
/ event
references to an object of that class, std::ofstream
std::ostream&
, and initialize them with std::cout
or with std::ofstream{"filename.txt"}
via an appropriate constructor of the logging_class
.因此,正如评论中所建议的,您可以对 class、 std::ofstream
std::ostream&
的 object 进行error
/ event
引用,并使用std::cout
或使用std::ofstream{"filename.txt"}
通过logging_class
的适当构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.