Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

singleton for vector data?



  • Hi!.

    i have a form where user adds contacts, the form contains fields like name, phone and address.

    Each registered contact contains an unique register number, so i think a map is the key to go, where the first type is the contact class

    What about creating a singleton containing this map ? so every form can get an instance of this unique map?



  • Hi U7Development,

    That's not a bad idea to make it a singleton, but this kind of consideration mainly depends on the other needs in your application.
    The main question is : will you need, in the future, to store contacts in another separated container for any reason ?

    About the map container, IMO, it is the right way.

    bye :)


  • Lifetime Qt Champion

    Hi
    Singletons are not really good design for sharing data in most cases as its
    more a global variable than anything else.

    It's better just to give the map a parameter to the classes that need it.

    so in main class or main.cpp
    std::map<Qstring, Contact > Contacts;

    and then
    simply

    MyDialog *Dia = new MyDialog (this, Contacts ) ;
    and you just change the default constructor to
    MyDialog (QWidget *parent , std::map<Qstring, Contact > & Contacts );

    and you can use it in that class.


Log in to reply