I encountered the problem during an interview. I had to create a multimap which had as keys objects with one private member(a string, the name of the object) and mapped value ints(not relevant). There was one restriction imposed by the person leading the interview: there should be not get or set functions associated to the member string of the object. After some processing of the multimap with STL functions(like removing some elements), the following was requested: to extract the name of the key objects into a vector while removing any duplicates. I do not know how to extract the name of the key objects without having a get function(I could remove the duplicates once the extraction is performed). The person leading the interview brought into discussion functors and possibility to use a functor which could be a friend of the Object class. Please let me know if you have any idea how this could be solved! I provide bellow a simplified version of the code creating the multimap.
class Object
{
std::string m_name; /* no set or get function allowed */
public:
Object(std::string name): m_name(name) {}
friend bool operator< (const Object& o1, const Object& o2)
{
return o1.m_name.compare(o2.m_name) < 0;
}
};
void testMap()
{
std::multimap<Object, int> m1;
m1.insert(std::make_pair(Object("abc"), 1));
m1.insert(std::make_pair(Object("qwerty"), 2));
m1.insert(std::make_pair(Object("def"), 3));
m1.insert(std::make_pair(Object("qwerty"), 4))
/* extract Objects names in a vector while removing duplicates without adding a get m_name function */
}
Please let me know if you have any idea how this could be solved! I do not know how to access m_name which is private without a get function...