Results 1 to 7 of 7

Thread: Question about singleton

  1. #1
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Question about singleton

    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++.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Question about singleton

    Using Qt does not suddenly make C++ stop working. It does not matter whether GlobalClass is derived from a Qt class or not.

  3. #3
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Question about singleton

    for example first thing i am not doing my _instance like a pointer. And i dont initialize it the first time instance is called.
    So i think this example that i did with instances is wrong.
    Qt Code:
    1. Code:
    2. #include <QCoreApplication>
    3. #include <QDebug>
    4. #include "downloader.h"
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. QObject::connect(Downloader::instance(),Downloader::testSignal,
    10. {
    11. });
    12. return a.exec();
    13. }
    14.  
    15. downloader.cpp:
    16. #include "downloader.h"
    17. #include <QDebug>
    18.  
    19.  
    20. Downloader &Downloader::instance()
    21. {
    22. Downloader _instance;
    23. return _instance;
    24. }
    25.  
    26. Downloader::Downloader(QObject *parent) : QObject(parent)
    27. {
    28. doSomething();
    29. }
    30.  
    31. void Downloader::doSomething()
    32. {
    33. //emit testSignal();
    34. qDebug() << "It's working!!!!";
    35. QString s="http://myweb/currxml.php";
    36. manager = new QNetworkAccessManager(this);
    37. manager->get(QNetworkRequest(QUrl(s)));
    38. emit instance().testSignal();
    39. }
    40.  
    41. void Downloader::replyFinished (QNetworkReply *reply)
    42. {
    43. if(reply->error())
    44. {
    45. qDebug() << "ERROR!";
    46. qDebug() << reply->errorString();
    47. }
    48. else
    49. {
    50. //qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
    51. //qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
    52. //qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
    53. //qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    54. //qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    55. QString p = reply->request().url().fileName();
    56. QFile *file = new QFile("C:/Users/moh/"+p);
    57. if(file->open(QFile::Append))
    58. {
    59. file->write(reply->readAll());
    60. file->flush();
    61. file->close();
    62. }
    63. delete file;
    64. }
    65.  
    66. reply->deleteLater();
    67. }
    68.  
    69. downloader.h:
    70. #ifndef Downloader_H
    71. #define Downloader_H
    72.  
    73. #include <QObject>
    74. #include <QNetworkAccessManager>
    75. #include <QNetworkRequest>
    76. #include <QNetworkReply>
    77. #include <QUrl>
    78. #include <QDateTime>
    79. #include <QFile>
    80. #include <QDebug>
    81. class Downloader : public QObject
    82. {
    83. Q_OBJECT
    84. Q_DISABLE_COPY(Downloader)
    85. public:
    86. Downloader &instance();
    87. explicit Downloader(QObject *parent = nullptr);
    88. //virtual ~Downloader(){}
    89.  
    90. void doSomething();
    91. signals:
    92.  
    93. void testSignal();
    94.  
    95. public slots:
    96. //void testSlot();
    97. void replyFinished (QNetworkReply *reply);
    98.  
    99. private:
    100. QNetworkAccessManager *manager;
    101.  
    102. };
    103.  
    104. #endif // Downloader_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by SirJonas; 22nd October 2016 at 12:34.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Question about singleton

    Using a local static is a valid option for doing a singleton.

    Again, as Chris67 already said, using Qt doesn't suddenly invalidate common C++ solutions.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Question about singleton

    Just a note on your Downloader::instance() function: it is returning a reference to a stack-based local variable. That local variable ceases to exist at the return of the function, invalidating the reference, and leading to undefined behaviour.

    Take a look here for a way to do a singleton C++ class without pointers:
    http://stackoverflow.com/questions/1...attern#1008289

  6. #6
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Question about singleton

    when you say:
    invalidating the reference, and leading to undefined behaviour.

    you mean that the code is wrong?
    Anyways i have one problem too because when i connect download my the file but only once time i call the signal

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Question about singleton

    Quote Originally Posted by SirJonas View Post
    you mean that the code is wrong?
    Yes. A reference must always refer to an object that exists
    Anyways i have one problem too because when i connect download my the file but only once time i call the signal
    The signal is emitted once (on a second downloader instance at line 38) during the construction of the object at line 22. Then the downloader ceases to exist at line 23. At at least I think it would if the instance() function ever returned... I do not think it will in your code as the constructor constructs another local downloader, which constructs another, etc.

Similar Threads

  1. Singleton or not
    By cszawisza in forum Newbie
    Replies: 1
    Last Post: 18th June 2014, 11:35
  2. Qt Singleton Class
    By alexismedina in forum Newbie
    Replies: 3
    Last Post: 22nd June 2010, 17:03
  3. Singleton Menu!
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 3
    Last Post: 2nd December 2009, 11:32
  4. Replies: 2
    Last Post: 8th October 2006, 17:49
  5. 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.