Qt C ++: global objects compared to the link chain

I am currently using a singleton pattern for some global objects in my application (Qt application for the Symbian environment). However, due to some problems ( C ++ checking for plain-colored pointers ), it seems like I should change the logic.

I have 3 classes (logger, settings and container for some temporary data) that I need to access through several different objects. Currently, they are all created using a singleton template. A logger is basically just one public Log () method with some internal logic, when the parameters and the container have several get / set methods with some additional logic (for example, QFileSystemWatcher). In addition, the logger and settings have some cross-reference (for example, the log needs some settings and errors in the log settings).

Currently, everything works "perfectly", but there are still some problems that should be taken care of, and it seems that they are not easy to implement for singletones (possible memory leaks / null pointers). Now I have two different ways to handle this:

  • Create global objects (e.g. extern Logger log;) and initialize them when the application starts.
  • Create objects in my main object and pass them as a reference.

As I have a few questions related to them:

Case 1

  • Is it better to use a stack or a bunch?
  • I am going to declare these objects in the globals.h header using the extern keyword. This is normal?
  • I think that in this case I need to delete this link in 2 directions (settings require a registrar and vice versa)?

Case 2

  • (, Logger * log = new Logger() vs Logger log;)

  • (, ).

  • ?

    • , ( , ): Children (Logger * log): m_Log (log), , ? m_Log NULL ?
    • , (Children (Logger & log): m_Log (log)), m_Log (Logger & m_Log;) ?
  • Qt ?

3. singleton ( ). - . . , . ?   #define Log Logger:: () →

.

+3
2

:

  • , singleton . , singleton ! Dan-O , , .
  • , , ( ).
  • , , , , (), (Logger, Settings ..), .
  • locals, # 3 , .

[] safe_static, . . , , , . (aka lazy loading) , , , , . , , , :

#include "log.h"

// declare your logger class here in the cpp file:
class Logger
{
// ... your impl as a singleton
}

void Log( const char* data )
{
    Logger.getInstance().DoRealLog( data );
}

, , , , ! , , . singleton, Log, , singleton. , : Logger ( , ). . singleton , -, , , - , .

(, extern Logger log;) .

, . , , , , ( ).

, :

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

:

// Logger::instance is a static method
Logger& Logger::instance()
{
    static Logger logger;
    return logger;
}

, safe_static. , , .

, , , . , , safe_static . - , , .

.

, . , .

?

, , . / ( / ) .

, , . , , . .

globals.h extern . ?

. @see safe_static .

, ( .)?

, , @see safe_static.

( , "" ): (Logger * log): m_Log () , ? m_Log to NULL ?

. , . , boost:: shared_ptr .

, ( (Logger & log): m_Log (log)), m_Log (Logger & m_Log;) ?

, . , , ( ) , , .

3. ( ). . . , . ?

boost:: scoped_ptr , , safe_static .

+1

. , , SO , :

?

, "" (, , ), , , . , , delete.

globals.h, extern. ?

? , , . , ala:

class c_Foo
{
    static c_Foo& Instance()
    {
        static c_Foo g_foo; // static local variable will live for full life of the program, but cannot be accessed elsewhere, forcing others to use cFoo::Instance()
        return g_foo;
    }
 };

, - c_Foo ( ) , .

, , "" (.. ), .

, ( )?

, , , .

(, Logger * log = new Logger() vs Logger log;)

, .

(, ).

?

, , . - , . "Log" ala C, , , . .cpp, . .

:

file: log.h

#ifndef LOG_H
#define LOG_H

void Log( const char* data ); // or QString or whatever you're passing to your logger

#endif//LOG_H

file: log.cpp

#include "log.h"

// declare your logger class here in the cpp file:
class Logger
{
// ... your impl as a singleton
}

void Log( const char* data )
{
    Logger.getInstance().DoRealLog( data );
}

, , .

. ?

, . .

, : http://blogs.msdn.com/b/oldnewthing/archive/2004/03/08/85901.aspx

-, , ( ), . , .

-2

Source: https://habr.com/ru/post/1752436/


All Articles