Results 1 to 8 of 8

Thread: Dynamic creation of Singleton

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Dynamic creation of Singleton

    sadly it seems there is no way to invoke a static method without a previous instance of the class.
    That's not true at all. Static methods do not require any class instance to invoke. You use the syntax:

    Qt Code:
    1. result = MyClass::myStaticMethod();
    To copy to clipboard, switch view to plain text mode 

    A common way to implement a Singleton class is to use a static class method along with a static pointer:

    Qt Code:
    1. // MyClass.h
    2. class MyClass
    3. {
    4. private:
    5. MyClass * pInstance;
    6.  
    7. MyClass();
    8. ~MyClass();
    9.  
    10. public:
    11. static MyClass * instance()
    12. {
    13. if ( !pInstance )
    14. pInstance = new MyClass();
    15.  
    16. return pInstance;
    17. }
    18.  
    19. };
    20.  
    21. // MyClass.cpp;
    22. MyClass::pInstance * instance = 0;
    23.  
    24. MyClass * pSingleton = MyClass::instance();
    To copy to clipboard, switch view to plain text mode 

    This leaves a dangling pInstance memory leak when the program exits, but you can implement code to clean that up if necessary.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #2
    Join Date
    Jan 2015
    Location
    Barquisimeto, Venezuela
    Posts
    24
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android
    Thanks
    2

    Default Re: Dynamic creation of Singleton

    Hi d_stranz.

    I fully understand what you say, I know how to call a static method.

    I am referring to the invocation of a static method without instance through QMetaObject :: InvokeMethod (), work with this class makes it impossible to invoke a static method without instance: s.

    But thanks for your good intentions

Similar Threads

  1. Dynamic creation of Scale object
    By Mookie in forum Qt Quick
    Replies: 4
    Last Post: 28th March 2016, 15:13
  2. Dynamic creation of list<transform>
    By Mookie in forum Qt Quick
    Replies: 1
    Last Post: 27th March 2016, 15:26
  3. QMetaType dynamic object creation and initialization
    By d_stranz in forum Qt Programming
    Replies: 5
    Last Post: 24th July 2014, 10:12
  4. Dynamic creation of widget with N images
    By davethomaspilot in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2014, 12:45
  5. Dynamic widget creation from an XML-like file
    By ttvo in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2009, 22:15

Tags for this Thread

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.