C++ const member functions

1.5k Views Asked by At

I have a syntax error that is puzzling

Previous code:

class A {
public:
    void process(const string& str) {};
};

I have

A a;
a.process("abcd");

all is well now I change the process member function to a const

void process(const string& str) const {};

and now a.process("abcd"); get a compile error about str being a const char[5]...

How the const addition impact the syntax error. I thought const only (in this context) meant that the member variables will not change?

Thoughts on this?

3

There are 3 best solutions below

0
On BEST ANSWER

Adding a const to the end of the method declaration would not have changed the semantics of the str parameter. Either something else is happening, or the compiler has a bug.

0
On

It should work just fine. Consequently, you have also made some other modifications that affect the result.

0
On

Adding a const qualifier to your member function means that object on which your calling your function can be const:

const A a;
a.process("abcd");

It doesn't have anything to do with your array of const chars.