passing ‘const this argument discards qualifiers [-fpermissive]

后端 未结 3 420
一向
一向 2020-12-29 02:03

I have a class Cache which has a function write specified as

bool write(const MemoryAccess &memory_access, CacheLine &cl);
相关标签:
3条回答
  • 2020-12-29 02:19

    When you call a method via a pointer to an object, this object is implicitly passed to the method as this pointer. c probably has type const Cache*. Since method write is not declared as const, it has non-const this pointer accessible from its body requiring const qualifier of c to be discarded.

    0 讨论(0)
  • 2020-12-29 02:32

    Also if your class's method returns pointer on any member you shouldn't forget write const before returning type example:

    const float * getPosition() const{...}

    0 讨论(0)
  • 2020-12-29 02:35

    Since c is of type const Cache *, you can only call const member functions on it.

    You have two options:

    (1) remove const from the declaration of c;

    (2) change Cache::write() like so:

     bool write(const MemoryAccess &memory_access, CacheLine &cl) const;
    

    (Note the added const at the end.)

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