468,512 Members | 1,606 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 2143
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bolin | last post: by
2 posts views Thread by Jianli Shen | last post: by
11 posts views Thread by Markus.Elfring | last post: by
10 posts views Thread by quantdev2004 | last post: by
34 posts views Thread by Perro Flaco | last post: by
16 posts views Thread by hzmonte | last post: by
2 posts views Thread by Jim Langston | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.