Results 1 to 7 of 7

Thread: Singleton pattern - end in recursion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Singleton pattern - end in recursion

    The mutex idea doesn't work either.

    In class Client.cpp
    Qt Code:
    1. QMutex mutex;
    2. mutex.lock();
    3. loginGui = LoginGui::loginGuiInstance();
    4. mutex.unlock();
    To copy to clipboard, switch view to plain text mode 


    In LoginGui.cpp
    Qt Code:
    1. QMutex mutex;
    2. mutex.lock();
    3. client = Client::clientInstance();
    4. mutex.unlock();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Singleton pattern - end in recursion

    Try this:

    Qt Code:
    1. class A {
    2. A();
    3. B *b;
    4. public:
    5. static A *instance();
    6. }
    7.  
    8. class B {
    9. B(A*);
    10. A *a;
    11. public:
    12. static B *instance(A *v=0);
    13. }
    14.  
    15. A::A(){
    16. b=0;
    17. }
    18.  
    19. B::B(A *v){
    20. a = v;
    21. }
    22.  
    23. A* A::instance(){
    24. static A *_inst=0;
    25. if(!_inst){
    26. _inst = new A();
    27. _inst->b = B::instance(_inst);
    28. }
    29. return _inst;
    30. }
    31.  
    32. B* B::instance(A *v){
    33. static B *_inst=0;
    34. if(_inst){
    35. _inst = new B(v);
    36. }
    37. return _inst;
    38. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. trying to use singleton pattern
    By therealjag in forum Newbie
    Replies: 3
    Last Post: 20th February 2006, 02:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.