Results 1 to 5 of 5

Thread: Variable question

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Variable question

    Hi to all, long time no see!!!

    I have a question now:

    In my project I will need variable that will hold the database connection. The variable will be initialized at application startup and all for objects in the application it is a must to see it. How and where do I put such a variable, that must be seen by all objects in an application? I want to follow c++ rules and I heard global variables are BAD idea. I also want to know why are global variables such a bad idea?
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Variable question

    Quote Originally Posted by MarkoSan View Post
    Hi to all, long time no see!!!

    I have a question now:

    In my project I will need variable that will hold the database connection. The variable will be initialized at application startup and all for objects in the application it is a must to see it. How and where do I put such a variable, that must be seen by all objects in an application? I want to follow c++ rules and I heard global variables are BAD idea. I also want to know why are global variables such a bad idea?
    You can make the variable static. Static variable applies to the class.

    Global variables are dangerous coz a global variable can potentially be modified from anywhere, and any part of the program may depend on it.

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Variable question

    Yes, but I need a variable that will be seen to all objects in an application, not just one object/class.
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Variable question

    Quote Originally Posted by MarkoSan View Post
    Yes, but I need a variable that will be seen to all objects in an application, not just one object/class.
    Try something like this

    Qt Code:
    1. //GlobalClass.h
    2.  
    3. // forward declaration
    4. class GlobalClass;
    5. // global GlobalClass object
    6. // there must only be one of these per application (like qApp)
    7. // defined and initialized in GlobalClass.cpp
    8. extern GlobalClass *chemGlobals;
    9.  
    10. class GlobalClass : public QObject
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. GlobalClass(int var = default){}
    16. virtual ~GlobalClass(){}
    17.  
    18. private:
    19. int m_val;
    20. };
    21.  
    22. ////GlobalClass.cpp
    23. #include "GlobalClass.h"
    24.  
    25. // global ChemGlobals object
    26. // there must only be one of these per application (like QApp)
    27. ChemGlobals * chemGlobals = 0;
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to vermarajeev for this useful post:

    MarkoSan (15th March 2007)

  6. #5
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Variable question

    It would be better if you try not using global variable. The singleton pattern would be a better candidate here. The advantages are many ...
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 15:34
  2. variable in method not initialized?!
    By frosch in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2006, 14:09
  3. Simple Question on Variable initialization
    By Naveen in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2006, 11:01
  4. custom plugin designer property with out a variable?
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 19:11
  5. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37

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.