Re: Qt and global variables
I think that depends on the design of your program. I would never use global variables. They are not thread safe. If you need a single instantiating object do something like this (which is also not thread safe):
Code:
class MyClass
{
MyClass() {};
virtual ~MyClass() {};
public:
static MyClass* instance()
{
if ( !m_pInstance )
m_pInstance = new MyClass;
return m_pInstance;
}
void doSomething()
{
// ...
}
protected:
static MyClass* m_pInstance;
};
MyClass* MyClass::m_pInstance = NULL;
Now you could use "MyClass* pointer = MyClass::instance(); pointer->doSomething();".
In that way you can also decide in which thread the instance will be created and no object exists before QApplication was instantiated. If you derive from QObject or not is your choice but don't forget to delete :)
Re: Qt and global variables
Quote:
Originally Posted by
Morea
Hi.
I was wondering how can I use global variables in my Qt program?
Why would you do that?
Yes, don't use them. Use the Singletton Pattern described by gri.
Cheers.
Re: Qt and global variables
Request you not to use global variables.
Try to stick on to true OOP concepts.
Qt is a wonderful framework which smoothly enables a
developer to create applications in true object domain.
I guess you are not trying to mix 'C' kind of coding using C++ compiler !
Re: Qt and global variables
The singleton code looks nice. I'll try to use that. Thanks!
Re: Qt and global variables
Quote:
Originally Posted by
Morea
The singleton code looks nice. I'll try to use that. Thanks!
The Singleton implementation described is used widely but also contains a race-condition when it comes to use in a multithreaded program (see Alexandrescu's "Modern C++ Design" for further details).
Code:
class MyClass
{
MyClass() {};
virtual ~MyClass() {};
public:
static MyClass* instance()
{
if ( !m_pInstance )
{
mutex.lock();
if (!m_pInstance)
m_pInstance = new MyClass;
mutex.unlock();
}
return m_pInstance;
}
void doSomething()
{
// ...
}
protected:
static MyClass* m_pInstance;
};
MyClass* MyClass::m_pInstance = NULL;
Re: Qt and global variables
Aye on the last post.
Singleton's are a great concept but just be aware that you have to pay careful attention to making them thread safe.
Re: Qt and global variables
Methedrine, would you mind putting the example on our wiki? The singleton pattern has been suggested/discussed several times on the forums. One could just refer to the wiki article in the future.. ;)
Re: Qt and global variables
Re: Qt and global variables
Sorry,
maybe I am too stupid, but your code doesn't work for me. I take your code and just changed the name of the class:
Code:
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
#include <QtCore>
#include <QtSql>
class configuration
{
public:
static configuration* instance()
{
if ( !m_instance )
{
mutex.lock();
if ( !m_instance )
m_instance = new configuration;
mutex.unlock();
}
return m_instance;
}
private:
configuration(){};
configuration( const configuration& _instance ){};
static configuration* m_instance;
// [...] some other functions
};
configuration* configuration::m_instance = NULL;
#endif
When compiling I receive the error (which I don't understand == don't know what to do/where the error could be):
.comp/sw_normal.o: In function `operator delete(void*, void*)':
/usr/include/qt4/QtCore/qatomic_i386.h:62: multiple definition of `configuration::m_instance'
.comp/qvortaro.o:/home/lykurg/Programmierung/qvortaro/src/qvortaro.cpp:194: first defined here
collect2: ld returned 1 exit status
And by the way must it in your example in the wiki not be
Code:
Singleton* Singleton::m_Instance = 0;
instead of
Code:
Singleton* m_Instance = 0;
Thanks,
Lykurg
P.s: if I comment the function (on bottom of the file) in qvortaro.cpp:194 out, then this error pointed to now last function on the file.
Re: Qt and global variables
Quote:
Originally Posted by
Lykurg
When compiling I receive the error (which I don't understand == don't know what to do/where the error could be):
.comp/sw_normal.o: In function `operator delete(void*, void*)':
/usr/include/qt4/QtCore/qatomic_i386.h:62: multiple definition of `configuration::m_instance'
.comp/qvortaro.o:/home/lykurg/Programmierung/qvortaro/src/qvortaro.cpp:194: first defined here
collect2: ld returned 1 exit status
You have placed the definition of static member variable in a header file, therefore every .cpp file that includes it has it's own copy of that variable. Better move that line to one of the .cpp files, say configuration.cpp.
Re: Qt and global variables
Quote:
Originally Posted by
Lykurg
And by the way must it in your example in the wiki not be
Code:
Singleton* Singleton::m_Instance = 0;
instead of
Code:
Singleton* m_Instance = 0;
Yes, of course. Thank you, Lykurg for spotting it, and thank you, jacek, for correcting it before I spotted this thread :)