【发布时间】:2010-10-22 20:19:59
【问题描述】:
(C++) 我有许多 Entry 类,并且有 BaseProcessor 接口,它封装了 Entry 处理逻辑。 (见下面的代码)
条目不提供运算符
我可以使用函数指针来比较程序中的 Entry 实例。但是,我需要为 Entry 类创建 std::set (或 std::map 或其他使用 less() 的东西)。我尝试使用 std::binary_function 派生类将其传递给 std::set,但看起来我无法将函数指针值传递给模板。
我该怎么做? C++03可以吗?
谢谢。
struct Entry
{
// ...
private:
bool operator< (const Entry &) const; // Should be defined by BaseProcessor.
};
typedef bool (*LessFunc)(const Entry &, const Entry &);
class BaseProcessor
{
public:
// ...
virtual LessFunc getLessFunc () const = 0;
};
// ...
BaseProcessor *processor = getProcessor();
LessFunc lessfunc = processor->getLessFunc();
Entry e1;
Entry e2;
bool isLess = lessfunc(e1, e2); // OK
typedef std::set<Entry, ???> EntrySetImpl; // how to use lessfunc here?
EntrySetImpl entries;
【问题讨论】:
标签: c++ templates function-pointers