468,291 Members | 1,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

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


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.

Sep 6 '06 #1
3 1935
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
Sep 6 '06 #2
"coala" <qi*********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
:
: 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
Sep 6 '06 #3
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
Sep 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.