[Insight-users] Dynamic casting between itk smart pointers

Karthik Krishnan karthik.krishnan at kitware.com
Mon Feb 25 10:00:21 EST 2008


On 2/25/08, Anja Ende <anja.ende at googlemail.com> wrote:
>
> Hi all,
>
> Sorry been bugging this list with questions today but just started out
> with ITK and am having some troubles...
>
> I have a class which has a member of itk::ProcessObject::Pointer type
> (m_extractImageFilter).
>
> Now, at some point it gets set to an itk::ExtractImageFilterType as
> follows:
>
> typedef typename itk::ExtractImageFilter<InputImageType, OutputImageType>
> ExtractImageFilterType;
> typedef typename ExtractImageFilterType::Pointer
> ExtractImageFilterTypePointer;
> ExtractImageFilterTypePointer extractImageFilter =
> ExtractImageFilterType::New();
> m_extractImageFilter = extractImageFilter;
>
> Now again, at some point in the code, I want to cast it back and I try to
> do the following:
>
> typedef typename itk::ExtractImageFilter<InputImageType, OutputImageType>
> ExtractImageFilterType;
> typedef typename ExtractImageFilterType::Pointer
> ExtractImageFilterTypePointer;
> ExtractImageFilterTypePointer extractImageFilter =
> dynamic_cast<ExtractImageFilterTypePointer>(m_extractImageFilter);
>
> The last line however fails with the following error:
>
> error: cannot dynamic_cast
> '((ITK2VTK*)this)->ITK2VTK::m_extractImageFilter' (of type 'class
> itk::SmartPointer<itk::ProcessObject>') to type 'class
> itk::SmartPointer<itk::ExtractImageFilter<itk::Image<double, 4u>,
> itk::Image<double, 3u> > >' (target is not pointer or reference)


You cannot dynamic_cast class A < X > to  A < Y > in C++ , even if Y derives
from X.  However you can certainly downcast X to Y.

So what you can do is.

ExtractImageFilterType * e = dynamic_cast< ExtractImageFilterType* >(
m_extractImageFilter.GetPointer() );
ExtractImageFilterTypePointer extractImageFilter = e;

Any ideas how I might safely do this?
>
> Cheers,
>
> Anja
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20080225/c49b633d/attachment.html


More information about the Insight-users mailing list