advice on using mutable

Discussion in 'C++' started by Ingo Nolden, Mar 7, 2005.

  1. Ingo Nolden

    Ingo Nolden Guest

    Hi,


    I try to use const where ever appropriate.
    In a collection class I am counting the iterators that are out. The
    counter decrements when an iterator leaves scope or is 'Dispose( )'d
    While iterators are around, any deleted item remains as NULL entry in
    the inner std::list

    This way I had to remove const from most of my member functions. Is is
    good style to make the counter mutable to avoid this, since the counter
    is not part of the main purpose of the class and its actual 'value'?

    The same applies to reference counting on ordinary objects. I use
    intrusive smart pointers, that increment and decrement a counter which
    is in the object itself. I couldn't change the counters value if the
    object is a const. Should the counter be mutable here?

    thanks in advance
    Ingo Nolden, Mar 7, 2005
    #1
    1. Advertising

  2. "Ingo Nolden" <> wrote in message
    news:d0igf0$ljc$-online.net...
    > I try to use const where ever appropriate.
    > In a collection class I am counting the iterators that are out. The
    > counter decrements when an iterator leaves scope or is 'Dispose( )'d
    > While iterators are around, any deleted item remains as NULL entry in the
    > inner std::list
    >
    > This way I had to remove const from most of my member functions. Is is
    > good style to make the counter mutable to avoid this, since the counter is
    > not part of the main purpose of the class and its actual 'value'?
    >
    > The same applies to reference counting on ordinary objects. I use
    > intrusive smart pointers, that increment and decrement a counter which is
    > in the object itself. I couldn't change the counters value if the object
    > is a const. Should the counter be mutable here?


    Yes. These would both be legitimate uses for 'mutable'.

    Another common reason to use mutable is for cached data (e.g. a 'getter'
    may store info about recent queries to re-execute them faster).



    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Mar 7, 2005
    #2
    1. Advertising

  3. Ingo Nolden

    Cy Edmunds Guest

    "Ingo Nolden" <> wrote in message
    news:d0igf0$ljc$-online.net...
    > Hi,
    >
    >
    > I try to use const where ever appropriate.
    > In a collection class I am counting the iterators that are out. The
    > counter decrements when an iterator leaves scope or is 'Dispose( )'d
    > While iterators are around, any deleted item remains as NULL entry in the
    > inner std::list
    >
    > This way I had to remove const from most of my member functions. Is is
    > good style to make the counter mutable to avoid this, since the counter is
    > not part of the main purpose of the class and its actual 'value'?
    >
    > The same applies to reference counting on ordinary objects. I use
    > intrusive smart pointers, that increment and decrement a counter which is
    > in the object itself. I couldn't change the counters value if the object
    > is a const. Should the counter be mutable here?
    >
    > thanks in advance


    A const method doesn't change the state of the object. However, the state of
    the object is an abstract concept, not at all the same as the private data
    within the class. What constitutes the state of the object is up to the
    object's designer. The applications of mutable you describe certainly sound
    OK to me.

    --
    Cy
    http://home.rochester.rr.com/cyhome/
    Cy Edmunds, Mar 8, 2005
    #3
  4. Ingo Nolden

    shabbir Guest

    The concept of mutable is very important. I give you an example of the
    scenario

    Lets assume that you are having a getty function and that function is
    assumed to be constant and so user expects that nothing is changing in
    the function. But for some reason there is a need of some elementary
    kind of Audit trail as who accessed the data and what time so we need
    to set the Date Accessed / Accesses By data of the const function and
    so we need variables that does not have influence of the User interface
    as Mutable and can be changed in the getty functions also.

    So I think you are right saying that
    "What constitutes the state of the object is up to the object's
    designer."

    but actually if we reframe it its how well the object has been designed
    to give the user a better interface like he is sure its constant
    functions is more important and some values that are for reference they
    can b changed.

    Thanks
    Shabbir Bhimani
    shabbir, Mar 8, 2005
    #4
    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. Asun Friere
    Replies:
    1
    Views:
    492
    Paul Boddie
    Aug 27, 2003
  2. Peter Hansen
    Replies:
    23
    Views:
    860
    Anton Vredegoor
    Sep 5, 2003
  3. Gerrit Holl
    Replies:
    16
    Views:
    612
    Tom Plunket
    Aug 29, 2003
  4. Asun Friere
    Replies:
    0
    Views:
    472
    Asun Friere
    Aug 28, 2003
  5. Michele Simionato
    Replies:
    2
    Views:
    381
    Jacek Generowicz
    Sep 1, 2003
Loading...

Share This Page