Results 1 to 4 of 4

Thread: purpose of Q_GLOBAL_STATIC

  1. #1
    Join Date
    Apr 2011
    Location
    Hyderabad, India
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default purpose of Q_GLOBAL_STATIC

    Whats the purpose of Q_GLOBAL_STATIC macro and why it is used for.
    Please follow thru some example.

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: purpose of Q_GLOBAL_STATIC

    When I taken a look how it looks like, it looks like it is working for singletons.
    First call of function NAME will cause creation of singleton and return pointer to that singleton.
    During application clean up this singleton still can be used. In case it was already destroyed function NAME will return null pointer (this will prevent use of dangling pointer).

    Macro is not documented so it shouldn't be used.

    I have doubts how it should be used. Problem is with static keyword.
    If macro is used outside of class in header file it will not be singleton, you will have one instance for each cpp file that included that header.
    If macro is used outside of class in source file it will be visible only in that cpp file.
    If used inside of class this will define inline static method className::NAME so again you will have multiple instances for each use of this method.
    So it doesn't look like this macro is very useful. Only in case when you want have singleton visible only in single source file.
    Last edited by MarekR22; 15th July 2011 at 13:01.

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: purpose of Q_GLOBAL_STATIC

    look at QThreadPool::globalInstance Its implemented with the macro. it can be used (short example) like this:

    header:

    Qt Code:
    1. class Example {
    2.  
    3. static Example* globalInstance();
    4. };
    To copy to clipboard, switch view to plain text mode 

    in cpp

    Qt Code:
    1. Q_GLOBAL_STATIC(Example, theInstance);
    2.  
    3. Example* Example::globalInstance() {
    4. return theInstance();
    5. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to nightghost for this useful post:

    raj_iv (15th July 2011)

  5. #4
    Join Date
    Apr 2011
    Location
    Hyderabad, India
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: purpose of Q_GLOBAL_STATIC

    thanx MarekR22

    yes i m using this macro only in case when i want to have singleton visible only in single source file(.cpp).

    as u said :-)

Similar Threads

  1. Navigation purpose
    By jerkymotion in forum Qt Programming
    Replies: 6
    Last Post: 17th March 2011, 17:59
  2. Using Q_GLOBAL_STATIC is the recomendent way?
    By oficjalne100 in forum Qt Programming
    Replies: 2
    Last Post: 10th February 2011, 22:42
  3. How to make a general purpose storage class?
    By khikho in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2009, 23:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.