lundi 27 avril 2015

How to declare / define once and use map list in two classes in program?

I'm developing a program, in C++11, in wich I need to use a C++ STL map in two classes. The map has, in the 2 places used, the same list of keys and values.

These classes are used in diferents parts of the program:

// in other part of the program
void check_something()
{
// the size of the list can be 100, 200 or ...
map<string, string> amap{
    {"something", "nothing"},
    {"key", "value"},
};

SameClass sc{amap};
// use sc
}

// in other part of the program
void check_other_thing()
{
// the size of the list can be 100, 200 or ...
map<string, string> amap_2{
    {"something", "nothing"},
    {"key", "value"},
};

SameClass sx{amap_2};
// use sx
}

For now this works (for testing), but I'd like to know (for maintenance and optimization) how to declare and define once and then use the map wherever I need in the program?

Do I need to create a class or struct or function that put the map in a shared_ptr<> and return the map?

I don't want to use global variables!

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire