I have 2 classes and one of them has map<string, vector<string> >I want to be able to use this in other class. Here is my code:
class a
{
map<string, vector<string> > m;
public:
const map<string, vector<string> > & get()
{
return m;
}
};
class b
{
a obj;
public:
void test()
{
map<string, vector<string> > m= obj.get();
// and then print
}
};
int main(int argc, char ** argv)
{
b bobj;
bobj.test();
return 0;
}
Is the way I returned reference to map in class a correct? It worked,but I just want to confirm if it was done properly / I got lucky / any other comments about the code.
Thank you for your help.
If you do not want to change the map in
b::test(), you should not make a copy of it:My objections:
Major:
a::get()should beconst:Minor: I would create an alias for the map’s type using
typedef.Minor: I can’t see at all what
bis for.Given these, my code would look like this: