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. All .cpp can access a filled map
Forum Updated to NodeBB v4.3 + New Features

All .cpp can access a filled map

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 796 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.
  • R Offline
    R Offline
    R3dp1ll
    wrote on last edited by
    #1

    Hi everybody,

    What is the easiest way to share a map (filled with data) between multiple .cpp? I need to have access to its data in different .cpp.
    I tried a global variable in one of the .cpp...
    I tried a pointer to the map...
    And if possible they all have access to the same map to avoid copies (because it may be heavy).

    Thank you.

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

      Hi,

      How will you be using that map ?

      Depending on that you can used the same principle as the model view paradigm implemented by Qt.

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

      1 Reply Last reply
      2
      • R Offline
        R Offline
        R3dp1ll
        wrote on last edited by R3dp1ll
        #3

        I'm not sure I understand the question: s
        I'm sorry this is my first program, I'm starting.

        In fact my map will be used to have int values (virtual key value, ex: 0x41); I will associate them with string names (ex: VirtualKey_A).

        Now, in several .cpp I have to use APIs and use those keys.

        I do not want to create a map by .cpp. I would like that when I code with my APIs, I can find the data in 1 single map, no matter the .cpp

        Does that answer the question or I am completely beside? :s

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

          Can you explain what you want to do with your application ?

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            R3dp1ll
            wrote on last edited by
            #5

            Macro / key remapper / Shortcut software, like logitech, G-Hotkey etc...

            If you want to know everything :
            I make a GUI with which the user can choose which keys he wants to associate an action, then the software saves his preferences in a txt or bat file, and then loads the preferences data into a map. And as it uses the map to set up and launch the macros.

            A simple software, it can serve as shortcut etc ... It's just a training for flux, data management, GUI.

            If you have other ideas for storing user preferences and allowing my different .cpp to access this data I am interested.

            jsulmJ 1 Reply Last reply
            0
            • R R3dp1ll

              Macro / key remapper / Shortcut software, like logitech, G-Hotkey etc...

              If you want to know everything :
              I make a GUI with which the user can choose which keys he wants to associate an action, then the software saves his preferences in a txt or bat file, and then loads the preferences data into a map. And as it uses the map to set up and launch the macros.

              A simple software, it can serve as shortcut etc ... It's just a training for flux, data management, GUI.

              If you have other ideas for storing user preferences and allowing my different .cpp to access this data I am interested.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #6

              @R3dp1ll There are so many ways to pass data between classes, using a global variable is the worst!
              Depending on your needs/design (that's why @SGaist is asking) you can pass it already to the constructor via reference/pointer or you can implement a setter method in the class which needs it and pass it again as reference/pointer.
              But if you share same data between different classes you should think more about your design - is it really needed? Classes should hide complexity and provide public interfaces to do something. It sounds like you should have this map only in one class without sharing it with others. Other classes can use some public method of that first class to get needed information without even knowing that there is a map (information hiding).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • R Offline
                R Offline
                R3dp1ll
                wrote on last edited by R3dp1ll
                #7

                What takes the least RAM and Processor resources:

                Create a map and send the pointers to multiple .cpp so that they can read / modify the data.

                Create a class and an object where the data will be stored in private and with public methods to modify them, and the .cpp will use these methods.

                There is a huge difference in terms of resource consumption?

                PS : I know that in terms of security : the class with value in private and method in public is the safest. But in fact, nobody will do anything if I use a map. The user will only have GUI access, and what the GUI does : I check it :)

                PS2 : In fact, I choose the use of classes. I'm here to learn the OOP, it will be a good exercise! So, my data that I wanted stored in the map, I will separate them in several classes. And if one of the classes really needs an info contained in another, it will go through public methods as you told me.

                And I think that for the speed to retrieve the information between the two methods it must be the same. The map receives a request, looks in its base and returns the value. The class does the same thing ... so it's the same (I think).

                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