473,804 Members | 3,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1582
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
3263
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 a table. The class has a pointer to the table. The store() method is constant since the class' members don't change. However, the table's store method is not constant, which is bringing
5
3065
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 nonconst members. Is there a way to get something like "mutable inheritance" to accomplish this?
2
1701
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(); class MyClass { otl_stream m_otl_stream; public:
3
2363
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 iterators are around, any deleted item remains as NULL entry in the inner std::list This way I had to remove const from most of my member functions. Is is
6
1748
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 have one and make the data representation (a std::valarray) mutable instead. My question is, how bad is that? Am I running the risk of undefined behaviour, or is the worst case scenario simply const violation? -- Christopher Diggins...
5
3361
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? Links? FAQ?
18
2601
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? http://david.tribble.com/text/cdiffs.htm#C99-cpp-keyword http://www.inf.uni-konstanz.de/~kuehl/c++-faq/const-correctness.html#faq-18.13 Regards, Markus
3
1456
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 the end ). class Cgroup_info: group_name = "" count = "0" #last time checked and processed/retrieved first = "0" last = "" retrieval_type = "" # allways , ask( if more than some limit), none date_checked = ""
2
4273
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 whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for 'set' and 'multiset', both the iterator and const_iterator types are constant bidirectional types -...
0
9584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.