Results 1 to 7 of 7

Thread: Using Singletton Pattern for global variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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, ...)

  2. #2
    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?

  3. #3
    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 

  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

    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: 9th June 2011, 23:28
  2. Replies: 4
    Last Post: 29th July 2010, 05:07
  3. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 1st February 2007, 23:42
  4. Global variables
    By Mariane in forum Newbie
    Replies: 14
    Last Post: 10th October 2006, 17:23
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19: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
  •  
Qt is a trademark of The Qt Company.