This is the code:
#include <map>
class Hidden {
private:
friend class Visible;
Hidden(); { /* nothing */ }
};
class Visible {
public:
void f() {
std::map<int, Hidden> m;
m[1] = Hidden(); // compilation error, class Hidden is private
}
};
The code doesn’t compile because the constructor of class Hidden is private for class std::map. Obviously, I don’t want to make class std::map a friend of Hidden. But what should I do here? Thanks in advance!
Add the map as a friend class:
Of course, it means you have to declare all Hidden users inside Hidden, but that’s exactly the point of the “private class” pattern you’re using…