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

前端 未结 2 2342
清酒与你
清酒与你 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:10

    You need to explicitly tell the compiler that your function will not modify any members:

    bool const select(Tuple const & tup) const {
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题