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