【问题标题】:how would you specialize std::greater你将如何专门化 std::greater
【发布时间】:2015-03-02 05:40:53
【问题描述】:

有一个

std::list<MyBaseClass*> objects;

如何(意味着语法)你会专门化 std::greater 对于 std::list 的排序方法

template<> class std::greater<MyBaseClass*> {
public:
     bool operator()(const MyBaseClass*& lhs, const MyBaseClass*& rhs) const
     {
         return lhs->get_index() > rhs->get_index();
     }

产生错误:

C2662:“int MyBaseClass::get_index(void)”:无法将“this”指针从“const MyBaseClass”转换为“MyBaseClass &” 转换失去限定符

你能解释一下这个问题吗?

【问题讨论】:

  • 为什么要专攻std::greater?为您的类或指向您的类的指针实施operator&gt;。您看到的错误是因为 get_index 不是 const 成员函数。
  • 你说得对,运营商 > 会更好。我会试着走这条路。无论如何,谢谢你的“const”它也可以工作,当然

标签: c++ stl


【解决方案1】:

eidos::MyBaseClass::get_index() 的实现需要标记为“const”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多