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 for vector data?
Forum Update on Monday, May 27th 2025

singleton for vector data?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 230 Views
  • 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.
  • U Offline
    U Offline
    U7Development
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jhx76
      wrote on last edited by
      #2

      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 :)

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        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.

        1 Reply Last reply
        1

        • Login

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