'this' argument to member function 'select' has type 'const SelectParam', but function is not marked const

前端 未结 2 2345
清酒与你
清酒与你 2021-01-07 05:34

I\'m trying to call a function on a polymorphic item. But I get the following error message at compile time:

\'this\' argument to member

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 06:18

    Indeed, you cannot call a non-const method a const object. But you also cannot call a non-const method through a pointer or reference to a const object (regardless of whether the referred-to object is const or not).

    This means that this:

    const SelectParam* ptr = whatever();
    ptr->select(someTuple);
    

    is ill-formed.

    In your case, you've declared a pointer to a const SelectParam here on this line:

    for (const SelectParam* p: selectionParams) {
    

    Simply remove the const and it should work :-)

    On the other hand, if select is never meant to modify the object, simply mark it as const:

    virtual const bool select( Tuple const & t) const = 0;
    

    And your code should also work.

提交回复
热议问题