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
Code:
#ifndef DATA_H
#define DATA_H
#include <QDebug>
class Dummy
{
public:
Dummy()
{
qDebug() << "Dummy constructor";
}
void show()
{
qDebug() << "Dummy show";
}
};
extern Dummy dummy;
#endif // DATA_H
data.cpp
Code:
#include "data.h"
Dummy dummy; // alternate defintion 1
main.cpp
Code:
#include "data.h"
Dummy dummy; // alternate definition 2
int main(int argc, char *argv[])
{
dummy.show();
return 0;
}
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.
Re: How to use an external variable ?
You may wish to research singletons.
Re: How to use an external variable ?
Hmm. Maybe it is just that everything works, but qDebug() doesn't. That would be consistent with the observation.
But why would that be ?
Re: How to use an external variable ?
both "alternate defintion 1" and "alternate defintion 2" are ok, either one of these will work, you should be getting the qDebug() messages, Under Windows, the message is sent to the console, if it is a console application; otherwise, it is sent to the debugger. (check "Application Ouput" tab is using Qt Creator)
Re: How to use an external variable ?
Quote:
Originally Posted by
Santosh Reddy
both "alternate defintion 1" and "alternate defintion 2" are ok, either one of these will work, you should be getting the qDebug() messages, Under Windows, the message is sent to the console, if it is a console application; otherwise, it is sent to the debugger. (check "Application Ouput" tab is using Qt Creator)
The qDebug() output from the Dummy class functions is not showing in Application Output.
But when I use qDebug() from my main function, e.g. if I add a public QString to the Dummy class, set its value in the Dummy constructor and access and output it with qDebug() from 'main', then it works.
I forgot to mention that I had done this too.
Quote:
Originally Posted by
squidge
You may wish to research singletons.
Well, I stumbled on this thread when posting this.
The singleton code looks a bit cumbersome to me. Does it have any intrinsic advantages or is it just a work-around to avoid global variables ? Are global variables just poorly implemented in C++ ?
I'd just like to understand why it's good to use singletons.
Re: How to use an external variable ?
does your .pro file anything more than this?
Code:
HEADERS += \
data.h
SOURCES += \
data.cpp \
main.cpp
Re: How to use an external variable ?
My project file looks like this:
Code:
#-------------------------------------------------
#
# Project created by QtCreator 2010-11-25T17:13:18
#
#-------------------------------------------------
QT += core gui
TARGET = Test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
data.cpp \
ssvector.cpp
HEADERS += mainwindow.h \
data.h \
ssvector.h
FORMS += mainwindow.ui
For the current test I've outcommented all calls to mainwindow and ssvector (in the source code), as they were from earlier tests and were only still in there for later use.
Re: How to use an external variable ?
test with the .pro file posted my me...
Re: How to use an external variable ?
qDebug() still doesn't work. Also, now the qDebug() in the main function doesn't work anymore.
Although that may be because at first I manipulated the .pro file myself.
When I did, Creator crashed, then I modified it again and it didn't crash anymore, but the main function qDebug() output was gone.
After that I copy-pasted your code and it's the same behaviour.
Added after 4 minutes:
I mean main function output like this:
qDebug() << dummy.str;
where 'str' is a public QString in Dummy initialized in the constructor. This used to show up correctly.
Re: How to use an external variable ?
About the problem that I cannot even use qDebug() in the 'main' function to output a public member QString from the Dummy class (or any other text for that matter): I cannot reliably reproduce this problem. It's there one moment, then I change something, undo the change and it suddenly works. It is apparently totally random. I have no clue.
But the problem that the qDebug() output from the Dummy class functions doesn't work is persistent.
I hope that if this problem is solved, the inconsistent behaviour of qDebug() in the 'main' function will disappear as well.
Re: How to use an external variable ?
Update/Conclusion:
I haven't figured out why qDebug() doesn't work for regular program runs anymore. The problem seems to be limited though to stuff that is initialized before the main function (external and static initializations), so maybe there's a reason for that.
Also, if I use the actual debugging option in Qt Creator, all of the qDebug() output shows up regularly.
So I can work with that.