Results 1 to 7 of 7

Thread: Singleton pattern - end in recursion

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

    Default Singleton pattern - end in recursion

    I have created a simple Singleton program. Is has 3 classes: main, Client, and LoginGui.

    main.cpp
    Qt Code:
    1. # include "logingui.h"
    2. # include "client.h"
    3. # include <iostream>
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8. cout << "Class main()\n";
    9. Client* client = Client::clientInstance();
    10. LoginGui* loginGui = LoginGui::loginGuiInstance();
    11. return 1;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Client.cpp
    Qt Code:
    1. # include "logingui.h"
    2. # include "client.h"
    3. # include <iostream>
    4. using namespace std;
    5.  
    6. Client* Client :: _clientInstance = 0;
    7.  
    8. Client* Client :: clientInstance()
    9. {
    10. if(_clientInstance == 0)
    11. {
    12. _clientInstance = new Client;
    13. }
    14. return _clientInstance;
    15. }
    16.  
    17.  
    18. Client :: Client()
    19. {
    20. cout << "class Client() - Constructor\n";
    21. numberInstances++;
    22. cout << "class Client() - numberInstances: " << numberInstances << "\n";
    23. loginGui = LoginGui::loginGuiInstance();
    24. }
    To copy to clipboard, switch view to plain text mode 

    LoginGui.cpp
    Qt Code:
    1. # include "client.h"
    2. # include "logingui.h"
    3. # include <iostream>
    4. using namespace std;
    5.  
    6. LoginGui* LoginGui:: _loginGuiInstance = 0;
    7.  
    8. LoginGui* LoginGui:: loginGuiInstance()
    9. {
    10. if(_loginGuiInstance == 0)
    11. {
    12. _loginGuiInstance = new LoginGui;
    13. }
    14. return _loginGuiInstance;
    15. }
    16.  
    17. LoginGui :: LoginGui()
    18. {
    19. cout << "class LoginGui() - Constructor\n";
    20. numberInstances++;
    21. cout << "class LoginGui() - numberInstances: " << numberInstances << "\n";
    22.  
    23. client1 = Client::clientInstance();
    24. }
    To copy to clipboard, switch view to plain text mode 


    The pattern is suppose to create only one instance of a class, but when the Client class calls the LoginGui class, it ends in recursion.

    Why ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Singleton pattern - end in recursion

    Quote Originally Posted by probine
    Why ?
    Because _clientInstance is 0. You will have to find some other way (than calling xxxInstance() in constructor) to connect those two classes.

    PS. Don't you think that three threads on the same subject is a bit too much?

  3. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Singleton pattern - end in recursion

    The classic singleton pattern has private contstructors, and provides a static instance() method. Use along with a mutex to prevent race conditions, and you're set.

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

    Default Re: Singleton pattern - end in recursion

    Sorry for the htree Threads in the same subject... I didn't know how to show the code with colors, therefore I post it again.

    The problem is that in main I instantiate Client and LoginGui, so when I try to instantiate them again, it shouldn't be aproblem, because the only thing I get back is the only an only one instance of the classes.

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

    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 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Singleton pattern - end in recursion

    Quote Originally Posted by probine
    Sorry for the htree Threads in the same subject... I didn't know how to show the code with colors, therefore I post it again.
    Next time, please, use the "Edit" button.

    Do you really need to obtain those instances in constructors?
    Try something like this:
    Qt Code:
    1. ClientGui::clientInstance()->setLoginGui( LoginGui::loginGuiInstance() );
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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, 01: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.