stl list, const correctness

J

Jim Strathmeyer

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.)
 
V

Victor Bazarov

Jim Strathmeyer said:
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
 
P

Pete Becker

Jim said:
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.
 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top