stl list, const correctness

Discussion in 'C++' started by Jim Strathmeyer, Mar 19, 2005.

  1. 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.)
     
    Jim Strathmeyer, Mar 19, 2005
    #1
    1. Advertising

  2. "Jim Strathmeyer" <> 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
     
    Victor Bazarov, Mar 19, 2005
    #2
    1. Advertising

  3. Jim Strathmeyer

    Pete Becker Guest

    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)
     
    Pete Becker, Mar 20, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler

    const-correctness and lambda expression

    Matthias Kaeppler, Apr 16, 2005, in forum: C++
    Replies:
    1
    Views:
    637
    Kanenas
    Apr 20, 2005
  2. Replies:
    10
    Views:
    590
    mlimber
    Oct 5, 2005
  3. const correctness

    , Oct 13, 2005, in forum: C++
    Replies:
    14
    Views:
    533
    Greg Comeau
    Oct 14, 2005
  4. Javier
    Replies:
    2
    Views:
    626
    James Kanze
    Sep 4, 2007
  5. fungus
    Replies:
    13
    Views:
    951
    fungus
    Oct 31, 2008
Loading...

Share This Page