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
#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
#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
To copy to clipboard, switch view to plain text mode
data.cpp
#include "data.h"
Dummy dummy; // alternate defintion 1
#include "data.h"
Dummy dummy; // alternate defintion 1
To copy to clipboard, switch view to plain text mode
main.cpp
#include "data.h"
Dummy dummy; // alternate definition 2
int main(int argc, char *argv[])
{
dummy.show();
return 0;
}
#include "data.h"
Dummy dummy; // alternate definition 2
int main(int argc, char *argv[])
{
dummy.show();
return 0;
}
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.
Bookmarks