Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Singleton pattern with MinGW

Singleton pattern with MinGW

Scheduled Pinned Locked Moved Solved General and Desktop
mingw64singleton
5 Posts 3 Posters 1.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jendker
    wrote on last edited by Jendker
    #1

    Hello everyone,

    I am right now switching from MSVC to MinGW and here proposed code for singleton does not work anymore producing following error in line 30 of singleton.h:

    singleton.h:30: error: invalid conversion from 'Singleton<Database>::CreateInstanceFunction {aka Database* (*)()}' to 'QBasicAtomicPointer<void>::Type {aka void*}' [-fpermissive]
       Singleton::create.store(create);
       ^~~~~~~~~
    qbasicatomic.h:240: note:   initializing argument 1 of 'void QBasicAtomicPointer<X>::store(QBasicAtomicPointer<X>::Type) [with X = void; QBasicAtomicPointer<X>::Type = void*]'
         void store(Type newValue) Q_DECL_NOTHROW { Ops::store(_q_value, newValue); }
              ^~~~~
    

    What would be the way to go around it with MinGW?

    EDIT: Maybe I am just creating the instance not properly to make it work with MinGW?
    I am calling it like this (creating instance of class Database):

    static inline Database* instance() { return singleton<Database>::instance(Database::createInstance); }
    

    this is my "interpretation" because the example in main of the example:

    // main.cpp
    
    #include <QTextStream>
    #include "myclass.h"
    
    #define MyClassInstance Singleton<MyClass>::instance()
    
    int main(int argc, char* argv[])
    {
        QTextStream(stdout) << MyClass::instance()->metaObject()->className() << endl;
        return 0;
    }
    

    was not clear to me.

    kshegunovK 1 Reply Last reply
    0
    • J Jendker

      Hello everyone,

      I am right now switching from MSVC to MinGW and here proposed code for singleton does not work anymore producing following error in line 30 of singleton.h:

      singleton.h:30: error: invalid conversion from 'Singleton<Database>::CreateInstanceFunction {aka Database* (*)()}' to 'QBasicAtomicPointer<void>::Type {aka void*}' [-fpermissive]
         Singleton::create.store(create);
         ^~~~~~~~~
      qbasicatomic.h:240: note:   initializing argument 1 of 'void QBasicAtomicPointer<X>::store(QBasicAtomicPointer<X>::Type) [with X = void; QBasicAtomicPointer<X>::Type = void*]'
           void store(Type newValue) Q_DECL_NOTHROW { Ops::store(_q_value, newValue); }
                ^~~~~
      

      What would be the way to go around it with MinGW?

      EDIT: Maybe I am just creating the instance not properly to make it work with MinGW?
      I am calling it like this (creating instance of class Database):

      static inline Database* instance() { return singleton<Database>::instance(Database::createInstance); }
      

      this is my "interpretation" because the example in main of the example:

      // main.cpp
      
      #include <QTextStream>
      #include "myclass.h"
      
      #define MyClassInstance Singleton<MyClass>::instance()
      
      int main(int argc, char* argv[])
      {
          QTextStream(stdout) << MyClass::instance()->metaObject()->className() << endl;
          return 0;
      }
      

      was not clear to me.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      The function pointer has be cast to a data pointer, so that should be done manually, which it isn't.
      But for the love of god, don't use that $%^@.

      If you're so intent on having something globally accessible, just use Q_GLOBAL_STATIC instead.

      Read and abide by the Qt Code of Conduct

      J 1 Reply Last reply
      2
      • kshegunovK kshegunov

        The function pointer has be cast to a data pointer, so that should be done manually, which it isn't.
        But for the love of god, don't use that $%^@.

        If you're so intent on having something globally accessible, just use Q_GLOBAL_STATIC instead.

        J Offline
        J Offline
        Jendker
        wrote on last edited by
        #3

        @kshegunov Thanks that solved it!

        Global is very useful in my case, previously I was passing the Database object from main through all the classes to have access to it whenever possible. This makes it much more reasonable. But I understand, that globals are generally evil :)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          If you are talking about a QSqlDatabase object then what you are doing is wrong. There's no need for any global object of that class as show in the details of the class.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          2
          • SGaistS SGaist

            Hi,

            If you are talking about a QSqlDatabase object then what you are doing is wrong. There's no need for any global object of that class as show in the details of the class.

            J Offline
            J Offline
            Jendker
            wrote on last edited by
            #5

            @SGaist Thank you, but this is actually the class where I am creating the tables when database is empty and holding all the models and necessary functions to work on them. So it is fine in this case :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved