Important: Please read the Qt Code of Conduct -

All .cpp can access a filled map

  • 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.

  • Lifetime Qt Champion


    How will you be using that map ?

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

  • 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

  • Lifetime Qt Champion

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

  • 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.

  • Lifetime Qt Champion

    @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).

  • 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).

Log in to reply