[solved] access data globally within application
-
wrote on 25 Sept 2015, 21:31 last edited by sachi
I am developing qt application which users can log. what is the best way to keep user details access globally within a application. global variable or static variable or there is another better way?
-
Hi,
It's not the role of static variables and global variables are rather frown upon. You should rather consider using a singleton for that kind of things
-
wrote on 26 Sept 2015, 05:38 last edited by
can you explain more about singleton and its implementation. may be tutorial about how to use singleton on qt?
-
wrote on 26 Sept 2015, 07:59 last edited by
The singleton is part of the software design patterns.
The implementation is easy: make the ctor private and implement one public static function which creates the instance:
class Foo { private: static Foo* _instance; Test(); public: static Foo* getInstance() { if (_instance==NULL) _instance = new Foo(); return _instance; }
cpp:
Foo::_instance = NULL;
And, please, put not all 'global variables' into one place, but consider the SOLID principles ;)
1/4