[Insight-users] Using Polymorphism with itk::KernelTransform

Luis Ibanez luis.ibanez@kitware.com
Fri, 24 Jan 2003 18:51:05 -0500


Hi Ofri,

In order to take advantage of the C++ polymorphism
you should return raw pointers instead of SmartPointers.
This situation is not exclusive of the KernelTransfom,
it applies to any other object deriving from itk::LightObject.

Returning raw pointers is done all over the toolkit. The
only precaution to take is to make sure that the raw pointer
is received by a SmartPointer or to make sure that there is no
reason for the object to be destroyed while its raw pointer
is still in use.

What you are doing is perfectly reasonable and conforms
to the typical use of ObjectFactories.

Here is a possible scenario:

typedef
itk::KernetTransform< MyCoordType,
                       MyDimension > BaseTransfomType;

BasetTransformType::Pointer myTransfom =
      createTransfom( sourcePoints, targetPoints, typeEnum );

With the function signature

    BasetTransformType * createTransform(...etc)

and, inside "createTransform()" you use some logic to decide
what type of transform to construct and return. You will
create this specific transform using its New() method and
pass the result to a local SmartPointer. Then, return the
raw pointer of this SmartPointer like

    BaseTrasnformType::Pointer newTransform
                           = SpecificTransform::New();

    return  newTrasform.GetPointer();


In this way, by the time the raw pointer is assigned to "myTransform"
the SmartPointer that was created in the function has been destroyed
and the reference count of the actual transform has been decremented
by 1. However, the reception of the transfrom in the SmartPointer
"myTransform" has also incremented the count by 1 and will keep the
transform alive.

At that point, you can use all the API of the BaseTransformType
and still be confident that the actual transform is a derived type
exercising polymorphism.


Please let us know if you find any problems,


   Thanks


      Luis



--------------------------------

Ofri Sadowsky wrote:
> Hi,
> 
> I have finally gone through the build process, and this time it's about
> the API.
> 
> Is it possible to use the polymorphism of itk::KernelTransform in the
> following way:
> 
> 
> itk::KernelTransform<MyCoordType, MyDimension> * createTransform(PointSet
> sourcePoints, PointSet targetPoints, int transformType)
> {
>   /* do something like switch over transformType and call the appropriate
>    * New() function, then return the new object as a result
>    */
> }
> 
> 
> 
> The problem I see is, that New() returns a ConcreteTransform::Pointer,
> which is derived from one or another smart-pointer templated types, and
> there will be no parallel derivation of ConcreteTransform::Pointer from
> KernelTransform::Pointer .  On the other hand, if I just return a
> KernelTransform * , I lose the smart pointer returned from New(), and then
> the KernelTransform object is destroyed.
> 
> So is there a way to use the KernelTransform abstraction properly?
> 
> Thanks,
> 
> Ofri.
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
> 
>