[Insight-developers] SmartPointers to const

Brad King brad.king@kitware.com
Fri, 19 Jan 2001 09:17:45 -0500 (EST)


I suggest we add this typedef to all itk classes:
typedef SmartPointer<const Self> ConstPointer;

That should allow smart pointers to reference const objects without much
effort.  The key to creating const smart pointers is to keep in mind that
the const keyword must apply to the object that is referenced, not the
pointer.  Therefore, the keyword should be included as part of the type
passed to the smart pointer's template parameter.

The only other problem is that the reference count of a const object
can't be updated.  To get around this, we need to make the methods
itk::LightObject::Register()
and
itk::LightObject::UnRegister()
both "const" functions.  Then, we add the "mutable" keyword to the
definition of the LightObject::m_ReferenceCount variable so that the const
methods can update it.  This way, a smart pointer to a const object will
be able to change the reference count, but nothing else.

The last interesting detail of this is that it is okay to make the
Register/UnRegister methods const even though they could potentially
delete the object.  It doesn't seem like deletion is very const to me.

-Brad

> 
> Hi,
> 
> Is there any way to use SmartPointers to const  objects ?
> or otherwise, is there a kind of ConstSmartPointer ?
> 
> That is, to have a SmartPointer addressing an object that 
> is not supposed to change.
> 
> I found that a SmartPointer can be defined from a normal 
> pointer just by assignment, but I didn't find a similar 
> method for a (const T *)
> 
> 
> 
> Thanks,
> 
> Luis
> 
> _______________________________________________
> Insight-developers mailing list
> Insight-developers@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-developers
>