mutable member and const method help

W

wtnt

Hello.

I previously had a program that compiled and worked with no error.
Relevant parts here:

class BasicList{
public:
char* listLookup() {
item = buffer;
...
return something;
}
protected:
char* item;
char buffer[128];
...
}

Now I try to make it const correct so I changed it to be this with the
const and mutable keywords:

class BasicList{
public:
char* listLookup() const{
item = buffer;
...
return something;
}
protected:
mutable char* item;
char buffer[128];
...
}

Now when I compile, I get an error at the assignment line
(item=buffer), saying invalid conversion from 'const char*' to
'char*'. I don't really understand why this error occurs, when it
didn't before the method was const. Why? and how to fix?

Your help is appreciated.
thank you!
 
A

Andrey Tarasevich

wtnt said:
...
Now I try to make it const correct so I changed it to be this with the
const and mutable keywords:

class BasicList{
public:
char* listLookup() const{
item = buffer;
...
return something;
}
protected:
mutable char* item;
char buffer[128];
...
}

Now when I compile, I get an error at the assignment line
(item=buffer), saying invalid conversion from 'const char*' to
'char*'. I don't really understand why this error occurs, when it
didn't before the method was const. Why? and how to fix?
...

Inside a 'const' method data member 'buffer' has type 'const char[128]'.
You are trying to convert it to type 'char*'. It is impossible. 'const
char[128]' can be converted to 'const char*', but not to 'char*'. That's
what the compiler is trying to tell you.

The immediate solution would be to declare 'buffer' mutable as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top