在对象使用前调用成员函数

发布于 2025-02-10 00:35:18 字数 751 浏览 2 评论 0原文

几天前,我是询问操作员对我的Logger项目的负载。现在,我还有另一个无法解决的问题 - 可能是由于我的经验低。

首先 - 我的logger对象(被设计为单身对象)应写入与源代码同一目录中创建的文件。所需的用途看起来像这样:

logger << "log message text";

因此,我创建了一个Singleton对象logger,但是我需要在第一个logger使用之前进行一些控件,例如,如果已经存在文件,则它是空的等。所有提供我已经创建的成员函数或我会的。

有什么意义?我需要以某种方式检查logger的第一次使用之前,请检查所有这些内容。我希望自动制作它们,因此可能的用户不必手动执行此操作。

因此,让我们总结一下...假设我想第一次使用它,所以我将此行放到我的代码中:

logger << "Log something";

在将日志本身制作之前,我需要logger class class检查是否存在

  • 日志文件已经存在
    • 如果不,创建一个
    • 如果是的,请找到结束并继续在下一行。

A few days ago, I was asking about operator overloading to my Logger project. Now I have another problem which I'm not able to solve - probably due to my low experience.

First - my Logger object (which is designed as a Singleton object) should write to a file created in the same directory as the source code. Desired use looks this way:

logger << "log message text";

So I have created a singleton object logger, but I need to make some controls before the first logger use, e.g. if the file already exists, if it is empty, etc. All the member functions which provide that I've already created or I will.

What's the point? I need somehow to check all these things before the first and every use of the logger. And I want them to be made automatically, so the possible user won't have to do that manually.

So, let´s summarize... let's say I want to use it for the first time, so I put this line to my code:

logger << "Log something";

Before it will make the log itself, I need the Logger class to check if:

  • the log file already exists
    • if no, create one
    • if yes, find the end and continue on the next line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2025-02-17 00:35:18

您的logger在哪里打开文件开始?您为什么不在打开文件的点上进行这些检查?

例如,这听起来像是您应该在Logger的构造函数中处理的东西。

并且,与其定义static方法,不要定义logger类,而不是定义全局logger对象,例如:

class Logger {
private:
    Logger() {
        // perform checks here...
        // open/create log file as needed...
    }

public:
    ~Logger() {
        // close log file...
    }

    static Logger& GetLogger() {
        static Logger logger;
        return logger;
    }

    // other methods/operators as needed...
};

...

Logger::GetLogger() << "Log something";

logger构造器直到第一次getLogger()才能运行。

Where does your Logger open the file to begin with? Why aren't you doing these checks at the point where the file is being opened?

This sounds like something you should be handling in your Logger's constructor, for instance.

And, rather than defining a global logger object, consider defining a static method in your Logger class instead, eg:

class Logger {
private:
    Logger() {
        // perform checks here...
        // open/create log file as needed...
    }

public:
    ~Logger() {
        // close log file...
    }

    static Logger& GetLogger() {
        static Logger logger;
        return logger;
    }

    // other methods/operators as needed...
};

...

Logger::GetLogger() << "Log something";

Then the Logger constructor won't run until the first time GetLogger() is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文