[Insight-developers] SmartPointer comparison operators

Luis Ibanez luis.ibanez@kitware.com
Thu, 09 May 2002 10:23:28 -0400


Aljaz,

Constructing and assigning SmartPointers require a Mutex operation.
This can be expensive in computing time.

We are trying to better localize the use of SmartPointers.
It seems that they are overused right now.

The place were they defintely have to be used is as receptors of
newly constructed types. For example:

   ImageType::Pointer    myNewImage = ImageType::New();


We are trying to avoid their use in cases where you can be confident
that the object in question will not go away while you are using it.
(For example, the itkCells in the itkMesh).

If you are creating a    std::set< SmartPointer< T > >  each insertion
on the set will cost you a construction of a SmartPointer. That can be
slow.  The SmartPointer will not allow you either to use polymorphism
directly, you will have to keep calling smartPointer.GetPointer() in
order to recover a raw pointer which can be polymorphic.

If you can be sure that somebody else is holding a SmartPointer
to these objects and this SmartPointer will not be destroyed while
you are using the std::set<>   it may be better to use a
std::set< T * >.

Using raw pointers in your set will provide better speed and also
allow polymorphism on the native C++ way.

At this moment we are changing all the GetOutput() and GetInput()
methods on the toolkit for returning raw pointers instead of SmartPointer.

Only those methods that return freshly constructed objects will be
returning SmartPointers.

Enforcement of const-correcteness on those raw pointers will be
left for a second pass   (Unfortunately....)


Luis


==============================================

Aljaz Noe wrote:

>No. But I'm using a const set<SmartPointer>, which means that operator< has
>to be defined.
>
>Otherwise I agree that this would be a very cool thing to have. It would be
>very simple to create containers for polymorphic objects. And we need a lot
>of those in FEM code...
>
>Aljaz
>