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

C

coala

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

Victor Bazarov

coala said:
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
 
I

Ivan Vecerina

:
: 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
 
C

coala

Yeah you guys are right, many thanks.

mPtr is mutable, I didn't notice that.

Sorry for that.


- C.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top