Results 1 to 15 of 15

Thread: Global variables

  1. #1
    Join Date
    Jan 2006
    Posts
    46
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Global variables

    I know I'm not supposed to use them, but they make life easier...

    Well, no so easy... :

    I started by writting in a file called globalVariables.h:
    Qt Code:
    1. #ifndef GLOBALVARIABLES_H
    2. #define GLOBALVARIABLES_H 0
    3. int ** Log;
    4. #endif
    To copy to clipboard, switch view to plain text mode 

    Then in maze.cpp and in mazeform.ui.h I did #include "globalVariables.h"
    (I tried to add globalVariables.h in maze.pro, then I removed it, it doesn't
    change anything). I get

    g++ -o maze .obj/maze.o .obj/mazebox.o .obj/main.o .obj/lcdrange.o .obj/mazeform.o .obj/qmake_image_collection.o .obj/moc_lcdrange.o .obj/moc_maze.o .obj/moc_mazeform.o -L/usr/share/qt3/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm
    .obj/mazeform.o(.bss+0x0): multiple definition of `Log'
    .obj/maze.o(.bss+0x0): first defined here
    collect2: ld returned 1 exit status
    make: *** [maze] Error 1

    BUT when I remove the #include directive from mazeform.ui.h, I get

    mazeform.ui.h: In member function `virtual void MazeForm::fileSave()':
    mazeform.ui.h:72: error: `GLOBALVARIABLES_H' undeclared (first use this
    function)
    mazeform.ui.h:72: error: (Each undeclared identifier is reported only once for
    each function it appears in.)
    mazeform.ui.h:73: error: `Log' undeclared (first use this function)
    make: *** [.obj/mazeform.o] Error 1

    (I was also testing to see if it knew GLOBALVARIABLES_H)

    AND when I remove the #include from maze.cpp I get

    maze.cpp: In member function `void MazeField::intialiseLog()':
    maze.cpp:89: error: `Log' undeclared (first use this function)
    maze.cpp:89: error: (Each undeclared identifier is reported only once for each
    function it appears in.)
    make: *** [.obj/maze.o] Error 1


    Please tell me WHERE I'm supposed to #include "globalVariables.h" so that
    all files have acces to it and it doesn't get included too often (ignoring the
    preprocessing directives?)???.

    Thank you

    Mariane
    Last edited by Mariane; 1st February 2006 at 17:16. Reason: Unwanted smiley caused by Field:: paintEvent

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    Quote Originally Posted by Mariane
    I started by writting in a file called globalVariables.h:
    Qt Code:
    1. #ifndef GLOBALVARIABLES_H
    2. #define GLOBALVARIABLES_H 0
    3. int ** Log;
    4. #endif
    To copy to clipboard, switch view to plain text mode 
    The problem is that you will have a different Log variable in every .cpp file that includes globalVariables.h, because that .h file contains definition of Log variable instead of forward declaration.

    What you need is:
    Qt Code:
    1. // globalVariables.h
    2. #ifndef GLOBALVARIABLES_H
    3. #define GLOBALVARIABLES_H
    4. extern int ** Log;
    5. #endif
    6.  
    7. // globalVariables.cpp
    8. int ** Log = 0;
    To copy to clipboard, switch view to plain text mode 

    But maybe you should consider using the singleton pattern?

  3. #3
    Join Date
    Jan 2006
    Posts
    46
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Global variables

    Thank you, I'll try this.

    What is a singleton pattern, please?

    Mariane

  4. #4
    Join Date
    Jan 2006
    Posts
    46
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Global variables

    It works! Thank you very very much

    Mariane

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    Quote Originally Posted by Mariane
    What is a singleton pattern, please?
    Singleton is just a way of hiding the fact that you use a global variable. It also ensures that you have at most one instance of that particular class.

    There are many ways to implement it, but it's usually something like this:
    Qt Code:
    1. class Singleton
    2. {
    3. public:
    4. static Singleton * instance()
    5. {
    6. if( _instance == 0 ) _instance = new Singleton();
    7. return _instance;
    8. }
    9.  
    10. // ...
    11.  
    12. private:
    13. Singleton() { /* ... */ }
    14.  
    15. static Singleton *_instance;
    16. };
    To copy to clipboard, switch view to plain text mode 
    You can use Singleton::instance() to access it (just like QApplication::instance() in Qt4).

  6. #6
    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: Global variables

    Instead of Using Singleton Pattern Why not define an class with Static get/set functions ?

    Qt Code:
    1. class GlobalData{
    2. public:
    3. static int userId(){
    4. return sm_iUserId;
    5. }
    6.  
    7. static void setUserId( int iUserId ){
    8. sm_iUserId = iUserId;
    9. }
    10.  
    11. private:
    12. static int sm_iUserId
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by sunil.thaha; 2nd February 2006 at 04:11.
    We can't solve problems by using the same kind of thinking we used when we created them

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    Quote Originally Posted by sunil.thaha
    Why not define an class with Static get/set functions ?
    Isn't that just a form of a singleton?

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    Quote Originally Posted by jacek
    Isn't that just a form of a singleton?
    Yes, static simply put is a global in a scope/namespace/class.

  9. #9
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    Quote Originally Posted by jacek
    Singleton is just a way of hiding the fact that you use a global variable...
    it's usually something like this...
    I also like this approach
    Qt Code:
    1. class Singleton
    2. {
    3. public:
    4. friend Singleton &instance()
    5. // ...
    6. void doSomething();
    7. private:
    8. Singleton() { /* ... */ }
    9. };
    10.  
    11. Singleton &SingletonInstance()
    12. {
    13. static Singleton aSingleton;
    14. return aSingleton;
    15. }
    To copy to clipboard, switch view to plain text mode 

    And the call to the instance is just SingletonInstace().doSomething()
    Both ways are really effective

  10. #10
    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: Global variables

    Quote Originally Posted by jacek
    Isn't that just a form of a singleton?
    Does your approach have any advantage ?
    We can't solve problems by using the same kind of thinking we used when we created them

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    Quote Originally Posted by sunil.thaha
    Does your approach have any advantage ?
    Yes, you can have a constructor (and with a little effort a destructor too).

  12. #12
    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: Global variables

    Quote Originally Posted by jacek
    Yes, you can have a constructor (and with a little effort a destructor too).
    Thought, it was a joke.
    But on second thought then You r Right

    We can't solve problems by using the same kind of thinking we used when we created them

  13. #13
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    Quote Originally Posted by sunil.thaha
    Thought, it was a joke.
    But on second thought then You r Right

    You can also use signals and slots with Jacek's implementation.
    Save yourself some pain. Learn C++ before learning Qt.

  14. The following user says thank you to Chicken Blood Machine for this useful post:

    sunil.thaha (28th February 2006)

  15. #14
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    Quote Originally Posted by jacek View Post
    Qt Code:
    1. class Singleton
    2. {
    3. public:
    4. static Singleton * instance()
    5. {
    6. if( _instance == 0 ) _instance = new Singleton();
    7. return _instance;
    8. }
    9.  
    10. // ...
    11.  
    12. private:
    13. Singleton() { /* ... */ }
    14.  
    15. static Singleton *_instance;
    16. };
    To copy to clipboard, switch view to plain text mode 
    It appears that this would need a delete() call at some point to prevent memory leaks. If it was subclassed from QObject wouldn't it be included as part of its garbage collection by calling:
    Qt Code:
    1. Singleton->setParent(qApp);
    To copy to clipboard, switch view to plain text mode 

    I understand it's an abstract example but I contemplating using singletons in some code and need to know how to handle their cleanup properly.
    Last edited by merlvingian; 9th October 2006 at 05:37. Reason: forgot the important part

  16. #15
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Global variables

    You can use qAddPostRoutine in <QApplication> to celan up non-QObject descendents.

    But, to be honest, a singleton is usually supposed to exist for the duration of the program execution. In these cases, it is not really a memory leak for the object to still exist at shutdown (unless it is important for some reason that the destructor is called).
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. QDevelop - global variables & help
    By impeteperry in forum Qt-based Software
    Replies: 2
    Last Post: 9th June 2011, 23:28
  2. QDevelop debuggng - viewing class member variables
    By dbrmik in forum Qt-based Software
    Replies: 0
    Last Post: 7th January 2009, 10:40
  3. creating query string from variables
    By locus in forum General Programming
    Replies: 2
    Last Post: 16th April 2007, 08:50
  4. Qt and global variables
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 1st February 2007, 23:42
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.