1

Google の助けを借りて、次のシングルトン ロギング クラスを作成しました。

class Log{
public:
    void Initialize(const char* fileName, int logLevel, ...);
    void outString(const char* str, ...);
    void outError(const char* str, ...);
    void outWarning(const char* str, ...);
    static Log* GetInstance() 
    {
        if (!m_instance)
            m_instance = new Log();
        return m_instance;
    }
private:
    Log() {}
    Log(const Log&);
    Log& operator=(const Log&); 
private:
    static Log *m_instance;
    void SetColor(bool stdout_stream, Color color);
    string getCurrentTime();
    void ResetColor(bool stdout_stream);
    int m_logLevel;
    ofstream *m_file;
};

ここで * が何であるかを知りたい: static Log *m_instance; なぜポインターとして設定するのですか? よくわかりません。つまり、それは何を指すのでしょうか?

4

2 に答える 2

2

クラスの唯一のインスタンスへのポインタです。

このインスタンスには、「Log::getInstance()」静的関数でアクセスできます。

コード内でクラスの複数のインスタンスを実際に必要としない場合は、シングルトン パターンを使用します。

于 2013-08-29T15:03:49.450 に答える