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 Updated to NodeBB v4.3 + New Features

singleton for vector data?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 236 Views 1 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.
  • U Offline
    U Offline
    U7Development
    wrote on 15 Nov 2019, 13:48 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 15 Nov 2019, 15:59 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
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 15 Nov 2019, 16:06 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

        1/3

        15 Nov 2019, 13:48

        • Login

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