passing ‘const this argument discards qualifiers [-fpermissive]

80,455

Solution 1

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.)

Solution 2

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.

Share:
80,455
prathmesh.kallurkar
Author by

prathmesh.kallurkar

Prathmesh Kallurkar is a research scholar in the Computer Science and Engg. department at IIT Delhi. He is working under the guidance of Dr. Smruti R. Sarangi towards his PhD thesis, "Architectural Support For Operating Systems". His primary research interests include futuristic computer architecture suited for operating systems, better OS designs, and, virtualization solutions for cloud. He is in the core developement team of the open source architectural simulator Tejas, and a member of the Srishti research group. He has instrumented the open source emulator Qemu to generate full system (includes operating system, and application) execution trace.

Updated on October 29, 2020

Comments

  • prathmesh.kallurkar
    prathmesh.kallurkar over 2 years

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

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

    I am calling this function like this.

    const Cache *this_cache;
    c = (a==b)?my_cache:not_cache;
    c->write(memory_access,cl);
    

    The above line is giving me following error

    "passing ‘const Cache’ as ‘this’ argument of ‘bool Cache::write(const MemoryAccess&, CacheLine&)’ discards qualifiers [-fpermissive]."

    the this argument is compiler specific which helps in code-mangling and breaking local namespace variable priority. But such a variable is not being passed here.

  • prathmesh.kallurkar
    prathmesh.kallurkar almost 11 years
    what is the significance of const at the end of the signature ?? What exactly do we mean when we say this is constant. Should it not be constant by default ??
  • NPE
    NPE almost 11 years