Reputation: 105103
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!
Upvotes: 3
Views: 552
Reputation: 2778
For your class Hidden, map is just another class and unless you explicitly make map a friend of Hidden, it map won't be able to access Hidden's constructor.
The approach given by paercebal must work as he's making map the friend of your class Hidden.
Upvotes: 0
Reputation: 83359
Add the map as a friend class:
#include <map>
class Hidden {
private:
friend class Visible;
friend class std::map<int, Hidden> ;
Hidden() {}
};
class Visible {
public:
void f() {
std::map<int, Hidden> m;
m[1] = Hidden(); // compilation error, class Hidden is private
}
};
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...
Upvotes: 2