[Insight-developers] Downcasting smartpointers
Luis Ibanez
luis.ibanez at kitware.com
Wed Sep 1 10:22:30 EDT 2010
Given that the dynamic_cast may return a NULL pointer
if the type is not convertable.
A safer way than
typedef itk::ImageToVTKImageFilter<InputImageType> itkFilterType;
( dynamic_cast<itkFilterType*>(m_filter.GetPointer())
)->SetInput(..etc...etc...
is to do:
typedef itk::ImageToVTKImageFilter<InputImageType> itkFilterType;
itkFilterType * filter =dynamic_cast<itkFilterType*>(m_filter.GetPointer())
if( filter )
{
filter->SetInput(..etc...etc...);
}
This is also more maintainable code.
Luis
-------------------------------------------------------
On Mon, Aug 30, 2010 at 2:36 PM, Matthew McCormick (thewtex) <
matt at mmmccormick.com> wrote:
>
>
> On Mon, Aug 30, 2010 at 1:02 PM, Antonin Perrot-Audet <
> antonin07130 at gmail.com> wrote:
>
>> Hello Matt,
>>
>>
>> On 8/30/10 1:45 PM, Matthew McCormick (thewtex) wrote:
>>
>> Hey Antonin
>>
>> On Mon, Aug 30, 2010 at 12:33 PM, Antonin Perrot-Audet <
>> antonin07130 at gmail.com> wrote:
>>
>>> Hello,
>>> I am trying to cast a processObject to an itkfilter, and couldn't manage
>>> to, here is the type of conversion I try to perform :
>>>
>>> typedef itk::ImageToVTKImageFilter<InputImageType>::Pointer
>>> itkFilterPointerType;
>>> typedef itk::ProcessObject::Pointer itkProcessObjectPointerType;
>>>
>>> itkProcessObjectPointerType m_filter = itkFilterPointerType::New();
>>>
>>> ( dynamic_cast<itkFilterPointerType>(itkFilterPointerType)
>>> )->SetInput(..etc...etc...
>>>
>>>
>> How about this:
>> ( dynamic_cast<itkFilterPointerType>(m_filter.GetPointer())
>> )->SetInput(..etc...etc...
>>
>>
>> thanks for your answer,
>> but m_filter.GetPointer() returns an
>> itk::ImageToVTKImageFilter<InputImageType>* and not a
>> itk::ImageToVTKImageFilter<InputImageType>::Pointer ,
>> so that I still get a cast error (cant cast from smart pointer to
>> pointer), but,
>> that works :
>>
>> typedef itk::ImageToVTKImageFilter<InputImageType> itkFilterType;
>> ( dynamic_cast<itkFilterType*>(m_filter.GetPointer())
>> )->SetInput(..etc...etc...
>>
>
> Oops -- thanks for the correction on changing the type to convert to.
>
>
>>
>> But then, I am not really manipulating a smart pointer, am I ?
>>
>>
> No, as far as I know, the casting operators can only be performed on raw
> pointers instead of SmartPointer objects.
>
> _______________________________________________
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20100901/b44214bf/attachment.htm>
More information about the Insight-developers
mailing list