473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

mutable member and const method help

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!
Jul 22 '05 #1
1 1557
wtnt wrote:
...
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thomas Matthews | last post by:
Hi, I understand that a member function can be declared as const {meaning it doesn't alter the member variables), but can it be declared as mutable? I have class that stores its members into...
5
by: Adam H. Peterson | last post by:
I'm using a private base class as part of the implementation for one of my classes. In my implementation, it holds cached data, and I'd like to be able to call its const member functions from my...
2
by: pit3k | last post by:
How can I call non-const member function of a member of a class in one of it's const qualified member function so that a compiler won't complain? Here's the code: bool otl_stream::eof(); ...
3
by: Ingo Nolden | last post by:
Hi, I try to use const where ever appropriate. In a collection class I am counting the iterators that are out. The counter decrements when an iterator leaves scope or is 'Dispose( )'d While...
6
by: christopher diggins | last post by:
I wrote a dynamic matrix class similar to the one described in TCPL 3rd Edition. Rather than define two separate iterators for const and non-const scenarios I decided to be a lazy bastard and only...
5
by: Pelle Beckman | last post by:
Hi, Honestly, what is this 'size_t'? And while I'm at it - what is a 'mutable' var.? I've understood it somehow makes a const a non-const, but what's the point of that? Short explanations?...
18
by: Markus.Elfring | last post by:
The C++ language specification provides the key word "mutable" that is not available in the C99 standard. Will it be imported to reduce any incompatibilities?...
3
by: Sambo | last post by:
By accident I assigned int to a class member 'count' which was initialized to (empty) string and had no error till I tried to use it as string, obviously. Why was there no error on assignment( near...
2
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.