C++ const

Const Correctness / C vs. C++ Differences
error: passing xxx as ‘this’ argument of xxx discards qualifiers

you’re calling a non-const member function on const object which is not allowed because non-const member functions make NO PROMISE not to modify the object

What is the meaning of ‘const’ at the end of a member function declaration?
declaring a const instance of a class

error: passing xxx as 'this' argument of xxx discards qualifiers
error: passing ‘const Foo’ as ‘this’ argument of ‘void Foo::bar()’ discards qualifiers [-fpermissive]
class Foo {
  public:
    Foo() {};
    virtual ~Foo() {};
    
    // must be const:
    // void bar() const {};
    void bar() {};
};

int
main(int argc, char *argv[])
{
  const Foo foo;

  foo.bar();

  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *