Smart Pointer release() const : it can set the pointer to null with the keyword "const"?

Discussion in 'C++' started by coala, Sep 6, 2006.

  1. coala

    coala Guest

    In Smart Pointer class, we have a piece of code:

    inline void release(void) const { if (mPtr) mPtr->release(); mPtr = 0;
    }

    It can set mPtr but it is still const?

    Why could this work and why it can be compiled? We have used it for
    years.

    - C.
    coala, Sep 6, 2006
    #1
    1. Advertising

  2. coala wrote:
    > In Smart Pointer class, we have a piece of code:
    >
    > inline void release(void) const { if (mPtr) mPtr->release(); mPtr = 0;
    > }
    >
    > It can set mPtr but it is still const?


    How is 'mPtr' declared?

    > Why could this work and why it can be compiled? We have used it for
    > years.


    You've used it for years and haven't looked at the declaration? Post
    the complete program distilled from your "Smart Pointer class", with
    the declaration of 'mPtr' (I presume it's a data member) visible, then
    we can talk.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 6, 2006
    #2
    1. Advertising

  3. "coala" <> wrote in message
    news:...
    :
    : In Smart Pointer class, we have a piece of code:
    :
    : inline void release(void) const { if (mPtr) mPtr->release(); mPtr = 0;
    : }
    :
    : It can set mPtr but it is still const?
    If mPtr is a data member, this will only compile if mPtr
    has been declared as mutable:
    T* mutable mPtr;


    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Sep 6, 2006
    #3
  4. coala

    coala Guest

    Re: Smart Pointer release() const : it can set the pointer to null with the keyword "const"?

    Yeah you guys are right, many thanks.

    mPtr is mutable, I didn't notice that.

    Sorry for that.


    - C.


    Victor Bazarov wrote:
    > coala wrote:
    > > In Smart Pointer class, we have a piece of code:
    > >
    > > inline void release(void) const { if (mPtr) mPtr->release(); mPtr = 0;
    > > }
    > >
    > > It can set mPtr but it is still const?

    >
    > How is 'mPtr' declared?
    >
    > > Why could this work and why it can be compiled? We have used it for
    > > years.

    >
    > You've used it for years and haven't looked at the declaration? Post
    > the complete program distilled from your "Smart Pointer class", with
    > the declaration of 'mPtr' (I presume it's a data member) visible, then
    > we can talk.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    coala, Sep 6, 2006
    #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. coala
    Replies:
    1
    Views:
    587
    Victor Bazarov
    Sep 6, 2006
  2. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  3. aneuryzma
    Replies:
    3
    Views:
    704
    Jim Langston
    Jun 16, 2008
  4. Hicham Mouline
    Replies:
    100
    Views:
    2,074
    Noah Roberts
    Aug 25, 2009
  5. Christopher
    Replies:
    4
    Views:
    440
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page