Results 1 to 11 of 11

Thread: How to use an external variable ?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    26
    Thanked 1 Time in 1 Post

    Default How to use an external variable ?

    Hi,

    I want to make a couple of generic data fields program-wide available. I tried to pack them all into just a few global variables, but I don't see how I can get rid of a global variable altogether. Is there a better way ?
    Or rather, is there a quick better way ? I just need to get this program functional for now.
    Currently I'm using global extern variables like this:

    data.h
    Qt Code:
    1. #ifndef DATA_H
    2. #define DATA_H
    3.  
    4. #include <QDebug>
    5.  
    6. class Dummy
    7. {
    8. public:
    9. Dummy()
    10. {
    11. qDebug() << "Dummy constructor";
    12. }
    13.  
    14. void show()
    15. {
    16. qDebug() << "Dummy show";
    17. }
    18. };
    19.  
    20. extern Dummy dummy;
    21.  
    22. #endif // DATA_H
    To copy to clipboard, switch view to plain text mode 

    data.cpp
    Qt Code:
    1. #include "data.h"
    2.  
    3. Dummy dummy; // alternate defintion 1
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "data.h"
    2.  
    3. Dummy dummy; // alternate definition 2
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. dummy.show();
    8.  
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that I wonder if the 'dummy' variable is actually ever initialized. The compiler doesn't complain, but neither of the qDebug() outputs is ever shown.
    I tried putting the definition of the dummy variable in the data.cpp and the main.cpp files, respectively. But no effect.

    However, if I put illegal code in the contrustor, the program does crash. (Though only if I put the definition of 'dummy' in 'data.cpp', not if it's in 'main.cpp'.)
    So that suggests that the constructor is in fact called.

    I may be just missing something very simple. But I can't conceive of it right now.

    Thanks for any help.
    Last edited by Computer Hater; 23rd June 2011 at 00:03.

Similar Threads

  1. variable
    By Atuti2009 in forum Qt Programming
    Replies: 8
    Last Post: 18th November 2009, 10:04
  2. Signal from variable
    By waynew in forum Newbie
    Replies: 2
    Last Post: 18th November 2009, 02:18
  3. QT and variable scope
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 22:32
  4. Getting Type of an variable
    By hgedek in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2007, 16:37
  5. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 05:19

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
  •  
Qt is a trademark of The Qt Company.