问题描述
我在我的C ++应用程序中有一个Logger类。这个类有像 writeDebug(std :: string& str)
这样的公共方法,可以写入调试输出文件,该对象正在由第一个主类在应用程序启动时创建,并且作为 Logger * myLogger;
对象存储。
由于我还有其他类,我想为这些类提供这种日志功能。
目前我使用myLogger指针指向其结构中的toher类,所有其他类在创建时存储这个非常相同的Logger指针 - >所有其他类都有 Logger * myLogger
指针存储:
Other_XY_Class :: Other_XY_Class(Logger * logger,...)
:localLoggerPtr {logger} {// ..}
==> localLoggerPtr(在类Other_XY_Class中)== myLogger(在主应用程序类中)
我可以想象有一个更方便/
因为你只有一个实例,为什么不使用Singleton呢?
这行代码:
class foo {
private:
void bar();
static foo实例;
public:
static void Bar(){Instance.bar(); }
}
I have a Logger class in my C++ application. This class has public methods like writeDebug(std::string & str)
, can write to a debug output file and it works for me very good. The object is being created on application start by the first main class and there it is stored as a Logger * myLogger;
object.
Since I have other classes as well, I would like to provide this logging functionality to those classes as well.
Currently I oberhead the myLogger pointer to toher classes in their contructor, and all the other classes store this very same Logger pointer when being created --> all other classes has this Logger * myLogger
pointer stored:
Other_XY_Class::Other_XY_Class(Logger * logger, ...)
: localLoggerPtr{logger} { //.. }
==> localLoggerPtr (in class Other_XY_Class) == myLogger (in main application class)
I can imagine that there is a more convenient / elegant way to handle that ?
Since you only have one instance, why not use a Singleton for that?
Something along this lines:
class foo {
private:
void bar();
static foo Instance;
public:
static void Bar() { Instance.bar(); }
}
这篇关于指向记录器类的指针是否为所有其他类提供?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!