473,246 Members | 1,304 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,246 software developers and data experts.

stl list, const correctness

I have a weird question about const correctness when using an stl list.
I have a wrapper Inventory class that holds a list of pointers to
Items. (Yes, they have to be pointers.)

Now, obviously the Inventory class isn't going to mutate the Items, so
its Add function should be Add(const Item *), and the list should be
std::list<const Item *>. One way to access the Inventory's item's is to
iterate through them with:

std::list<const Item *>::const_iterator ListBegin() const;
std::list<const Item *>::const_iterator ListEnd() const;

However, I obviously would like to mutate the items I get while
iterating through the Inventory's list. What am I doing wrong? My
currently implementation is the above without any const's, but obviously
I would like to encapsulate the fact that Inventory doesn't mutate its
items, and I would mostly like to understand how to do this correctly.

(Please don't try to convince me to use references instead of pointers,
or to not encapsulate std::list. I have my reasons, and I know they're
sound. Unfortunately, they didn't teach us things like const correctness
in school.)
Jul 23 '05 #1
2 2316
"Jim Strathmeyer" <st****@ipass.net> wrote...
I have a weird question about const correctness when using an stl list.
Weird indeed.
I have a wrapper Inventory class that holds a list of pointers to Items.
(Yes, they have to be pointers.)

Now, obviously the Inventory class isn't going to mutate the Items
OK, so, it doesn't mutate them. Good.
, so its Add function should be Add(const Item *), and the list should be
std::list<const Item *>. One way to access the Inventory's item's is to
iterate through them with:

std::list<const Item *>::const_iterator ListBegin() const;
std::list<const Item *>::const_iterator ListEnd() const;

However, I obviously would like to mutate the items
Wait, didn't you just say that the Inventory isn't going to mutate them?
So, which is it? Do you need to mutate them or don't you? Or do _you_
need to mutate them but you don't trust the Inventory to do it? Has it
let you down before? I mean those Inventory objects can be really tricky
to handle...
I get while iterating through the Inventory's list. What am I doing wrong?
So far you're just muddying the waters with "isn't going to" and "would
like to" WRT mutating the same items. You should stick to one of them.
My currently implementation is the above without any const's, but
obviously I would like to encapsulate the fact that Inventory doesn't
mutate its items, and I would mostly like to understand how to do this
correctly.
What happens if you do add 'const', but don't ask for 'const_iterator'?

What happens if you simply add 'const' and let it be? Do you get a compile
error? If so, what error do you get? Perhaps you could distill your code
to the minimal amount where you can demonstrate the actual problem you're
having with const-correctness...
(Please don't try to convince me to use references instead of pointers,
References cannot be stored in a container, so there is no reason for us
to try to convince you to use references. It simply cannot be done.
or to not encapsulate std::list. I have my reasons, and I know they're
sound. Unfortunately, they didn't teach us things like const correctness
in school.)


"Doctor, I am hearing voices inside my head. Don't try to tell me that
I am crazy. I know that I am not crazy. It's just I really hate those
gremlins who found their shelter between my ears." :-)

On a serious note, if you want our help you have to try to trust our
ability to help. That includes stating your "reasons" and testing out
their "soundness". In any case, post more code.

V
Jul 23 '05 #2
Jim Strathmeyer wrote:

However, I obviously would like to mutate the items I get while
iterating through the Inventory's list. What am I doing wrong?
const means "this object will never be modified through this access
path." Since your Inventory type provides an access path for
modification of its elements, the elements are not const.
My
currently implementation is the above without any const's


Right.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3

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

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
3
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But,...
2
by: Jianli Shen | last post by:
in a *.h file, there is a declaration: const ClassName *functionName() const {return oneVar;} I was confused by the two const there. can anybody help explain why we need the first const....
11
by: Markus.Elfring | last post by:
A couple of software provides const-incorrect programming interfaces. I guess that it is possible to develop const-correct APIs/SDKs from the beginning if a few basic design rules and patterns...
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
34
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
12
by: mast2as | last post by:
Hi everyone... I have a TExceptionHandler class that is uses in the code to thow exceptions. Whenever an exception is thrown the TExceptionHander constructor takes an error code (int) as an...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
2
by: Jim Langston | last post by:
In my function/method paramater list I try to keep constant correctness. I.E. I would use: int Foo( const char* a, int b ) instead of int Foo( char* a, int b ) My question reguards int b. ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...

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.