给定这个类:
class C
{
private:
struct Foo
{
int key1, key2, value;
};
std::vector<Foo> fooList;
};
这里的想法是fooList
可以由Foo 结构中的一个key1
或一个来索引。key2
我正在尝试编写要传递给的函子,以便我可以按每个键std::find_if
查找项目。fooList
但我无法让它们编译,因为Foo
在类中是私有的(它不是 C 接口的一部分)。 有没有办法在不暴露Foo
于世界其他地方的情况下做到这一点?
这是一个无法编译的代码示例,因为Foo
在我的类中是私有的:
struct MatchKey1 : public std::unary_function<Foo, bool>
{
int key;
MatchKey1(int k) : key(k) {}
bool operator()(const Foo& elem) const
{
return key == elem.key1;
}
};