Hi i'm using singleton but i did samemitakes when i use singleton. Normally i create static object. But this is not recomendable.
So someine can help me with this code i need to do this:
First thing i am trying to make my _instance like a pointer. Second i am trying initialize it the first time instance is called.
Example for c++:
Qt Code:
  1. class GlobalClass
  2. {
  3. int m_value;
  4. public:
  5. GlobalClass(int v = 0)
  6. {
  7. m_value = v;
  8. }
  9. int get_value()
  10. {
  11. return m_value;
  12. }
  13. void set_value(int v)
  14. {
  15. m_value = v;
  16. }
  17. };
  18.  
  19. // Default initialization
  20. GlobalClass *global_ptr = 0;
  21.  
  22. void foo(void)
  23. {
  24. // Initialization on first use
  25. if (!global_ptr)
  26. global_ptr = new GlobalClass;
  27. global_ptr->set_value(1);
  28. cout << "foo: global_ptr is " << global_ptr->get_value() << '\n';
  29. }
  30.  
  31. void bar(void)
  32. {
  33. if (!global_ptr)
  34. global_ptr = new GlobalClass;
  35. global_ptr->set_value(2);
  36. cout << "bar: global_ptr is " << global_ptr->get_value() << '\n';
  37. }
  38.  
  39. int main()
  40. {
  41. if (!global_ptr)
  42. global_ptr = new GlobalClass;
  43. cout << "main: global_ptr is " << global_ptr->get_value() << '\n';
  44. foo();
  45. bar();
  46. }
To copy to clipboard, switch view to plain text mode 

Some example similar for QT? I need some example very similiar to this example in c++.