[Insight-developers] MakeOutput : Raw pointer... Smart pointer ?

Antonin Perrot-Audet antonin07130 at gmail.com
Sat Apr 2 13:42:45 EDT 2011


Thank you very much Luis, for these explanations,
I now have a much better understanding of the GetPointer along ITK classes.

I did erase the unnecessary GetPointer() on my ITK branch.
Once I'll have KWStyle set up correctly, I'll submit tiny patches like that.

Best,

Antonin.


On 4/2/11 2:17 PM, Luis Ibanez wrote:
> Hi Antonin,
>
>
> 1) MakeOutput returns a SmartPointer,
>      the function signature is:
>
>     DataObjectPointer MakeOutput(unsigned int idx)
>
>
>
> 3) Smart Pointers do not support casting from
>      derived classes.
>
>      For example
>
>    typedef itk::ImageBase<  3>  ImageBaseType;
>    typedef itk::Image<  unsigned short, 3>  ImageType;
>    ImageType::Pointer image = ImageType::New();
>    ImageBaseType::Pointer baseImage = image;
>
>
> Will fail compilation with the message:
>
> conversion from ‘itk::SmartPointer<itk::Image<short unsigned int, 3u>  >’ to
>   non-scalar type ‘itk::SmartPointer<itk::ImageBase<3u>  >’ requested
>
>
> While the code:
>
>    typedef itk::ImageBase<  3>  ImageBaseType;
>    typedef itk::Image<  unsigned short, 3>  ImageType;
>    ImageType::Pointer image = ImageType::New();
>    ImageBaseType::Pointer baseImage = image.GetPointer();
>
>
> will compile and work fine.
>
> -----
>
> With SmartPointers you can't assign the smartpointer of a
> derived class to the smartpointer of a base class.
>
> With SmartPointers you can assigne the RAW pointer of a
> derived class to the smartpointer of a base class.
>
> That's the reason why GetPointer() is used in MakeOutput().
>
>
> 3) That said, the particular example that you are
>     looking at, has some unnecessary GetPointer() calls.
>     Particularly, the one in the return statement is not
>     needed.
>
>     The code:
>
>> ::MakeOutput(unsigned int idx)
>> {
>>    DataObject::Pointer output;
>>
>>    switch ( idx )
>>      {
>>      case 0:
>>        output = ( EigenValueImageType::New() ).GetPointer();
>>        break;
>>      case 1:
>>        output = ( EigenValueImageType::New() ).GetPointer();
>>        break;
>>      case 2:
>>        output = ( EigenVectorImageType::New() ).GetPointer();
>>        break;
>>      }
>>    return output.GetPointer();
>> }
>>
>      Should just be:
>
>> ::MakeOutput(unsigned int idx)
>> {
>>    DataObject::Pointer output;
>>
>>    switch ( idx )
>>      {
>>      case 0:
>>        output = ( EigenValueImageType::New() ).GetPointer();
>>        break;
>>      case 1:
>>        output = ( EigenValueImageType::New() ).GetPointer();
>>        break;
>>      case 2:
>>        output = ( EigenVectorImageType::New() ).GetPointer();
>>        break;
>>      }
>>    return output;
>> }
>>
>     Given that the "output" variable already
>     has the same type as the expected return
>     type of the function.
>
>     The calls
>
>       output = ( EigenValueImageType::New() ).GetPointer();
>
>     Need the GetPointer() method, because "output"
>     is a smart pointer to DataObject, and the
>     EigenValueImageType is a derived class from
>     DataObject.
>
>
>
>
>      Luis
>
>
>
> ----------------------------------------------
> On Fri, Apr 1, 2011 at 11:57 AM, Antonin Perrot-Audet
> <antonin07130 at gmail.com>  wrote:
>> Hello,
>>
>> I still have trouble understanding the function of
>> ProcessObject::MakeOutput
>> method.
>>
>> I have a filter with multiple outputs, and I must admit that I have trouble
>> understanding the principle of the example provided in
>> itkEigenAnalysis2DImageFilter
>> MakeOutput seems to *return a RAW pointer instead of a SMART POINTER* ?
>>
>> template<  class TInputImage, class TEigenValueImage, class TEigenVectorImage
>> DataObject::Pointer
>> EigenAnalysis2DImageFilter<  TInputImage, TEigenValueImage, TEigenVectorImage
>> ::MakeOutput(unsigned int idx)
>> {
>>    DataObject::Pointer output;
>>
>>    switch ( idx )
>>      {
>>      case 0:
>>        output = ( EigenValueImageType::New() ).GetPointer();
>>        break;
>>      case 1:
>>        output = ( EigenValueImageType::New() ).GetPointer();
>>        break;
>>      case 2:
>>        output = ( EigenVectorImageType::New() ).GetPointer();
>>        break;
>>      }
>>    return output.GetPointer();
>> }
>>
>> Can anyone give me more insights about *when to return smart pointers and
>> raw pointers* ?
>>
>> Thanks,
>> --
>> Antonin
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Kitware offers ITK Training Courses, for more information visit:
>> http://kitware.com/products/protraining.html
>>
>> Please keep messages on-topic and check the ITK FAQ at:
>> http://www.itk.org/Wiki/ITK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.itk.org/mailman/listinfo/insight-developers
>>
>>


-- 
Antonin Perrot-Audet
Electrical Engineering&  Computer Sciences, INSA Lyon
M2 GEGP Signal&  Image Processing, INSA Lyon, UCBL, Centrale Lyon



More information about the Insight-developers mailing list