Results 1 to 7 of 7

Thread: Using Singletton Pattern for global variables

  1. #1
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Using Singletton Pattern for global variables

    Hello you all!
    I read about Singletton Pattern in this post:
    http://www.qtcentre.org/threads/5326...obal-variables

    But if I'd like to use it to store a global variable (yes, I need it because I cannot pass it as an argument of function or other things like this) should I add only the variable in the class? as a static member?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Using Singletton Pattern for global variables

    Hi,

    if you want to use the singleton patters use the last version from our wiki with the mutex locker. Then best practice is to add a local private variable and use a getter and setter function. Also a public static const default variable is useful if you store user settings with QSettings.

    If you want to store a non changeable variable, i would just define a global variable without the singleton pattern.


    Lykurg

  3. #3
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using Singletton Pattern for global variables

    and how should I declare and define a global variable? in one-file-projects you can declare a variable out of the main after the #include (it's so ugly but it's possible). for multi-file-projects where do I put global variable declaration?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using Singletton Pattern for global variables

    The most obvious things are the hardest to figure out. That's because we puny humans always think too far.

    Just create a header file like mySuperDuperGlobalVariables.h
    Then include the header where you need it.
    Don't forget to include it once (use #ifndef, #define, ...)

  5. #5
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using Singletton Pattern for global variables

    Shall I add "static" to the variables so that all instances of them will edit the same phisical variable?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Using Singletton Pattern for global variables

    Just create a normal header file and include it in every file where you need that global variables. (Using namespaces is also a good choice to group your variables!)
    Qt Code:
    1. #ifndef ARTISTICSTYLECONSTANTS_H
    2. #define ARTISTICSTYLECONSTANTS_H
    3.  
    4. namespace ArtisticStyle
    5. {
    6. namespace Constants
    7. {
    8. // Predefined Style Options
    9. const char* const ASP_STYLE = "style";
    10. const int ASP_STYLE_DEF = -1;
    11.  
    12. // Tab and Bracket Options
    13. const char* const ASP_INDENT = "indent";
    14. const bool ASP_INDENT_DEF = true;
    15. const char* const ASP_INDENTSTYLE = "indentStyle";
    16. const char* const ASP_INDENTSTYLE_DEF = "spaces";
    17. const char* const ASP_INDENTSIZE = "indentSize";
    18. const int ASP_INDENTSIZE_DEF = 2;
    19. const char* const ASP_BRACKETS = "brackets";
    20. const bool ASP_BRACKETS_DEF = true;
    21. const char* const ASP_BRACKETSSTYLE = "bracketsStyle";
    22. const int ASP_BRACKETSSTYLE_DEF = astyle::BREAK_MODE;
    23.  
    24. // [...]
    25. }
    26. }
    27.  
    28. #endif // ARTISTICSTYLECONSTANTS_H
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using Singletton Pattern for global variables

    static and editing very rarely go together in one sentence.
    Except when pointing out that static is static and can't be edited.

    Qt Code:
    1. static int myStaticInt = 5;
    2. cout << "my static int =" << myStaticInt;
    To copy to clipboard, switch view to plain text mode 
    That's all you can do with a static variable.

Similar Threads

  1. QDevelop - global variables & help
    By impeteperry in forum Qt-based Software
    Replies: 2
    Last Post: 10th June 2011, 00:28
  2. Replies: 4
    Last Post: 29th July 2010, 06:07
  3. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 2nd February 2007, 00:42
  4. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 18:23
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 20:56

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.