[Insight-developers] Run-time polymorphism and Insight

Brad King brad.king at kitware.com
Wed Jun 14 10:25:50 EDT 2000


It would seem that not all compilers are capable of converting the smart
pointer type during the construction/initialization process.  On some
compilers, the code from Bill's example produces an error:

itkObject::Pointer o = itkImage::New();

This is not really a big deal, because there are easy solutions:

/**
  * This will work, and is useful if the pointer is re-used for new
  * object creation several times.
 */
itkObject::Pointer o;
/* ...other code... */
o = itkImage::New();

OR

/**
  * This is the proper way to initialize an object:
 */
itkObject::Pointer o(itkImage::New());

If all code is written this way, compiler support for smart-pointer
polymorphism will not be a problem.

On Tue, 13 Jun 2000, Bill Hoffman wrote:

> Do a cvs update and you will get a new itkSmartPointer that will support
> this.  After the update the following should work:
> 
> itkObject::Pointer o = itkImage::New();
> itkImage::Pointer i = dynamic_cast<itkObject*>(o);
> 
> We noticed the same problem here earlier this week, but did not check in
> the fix.  (the fix required providing a conversion operator 
> to a native pointer for the smartpointer class.  We had removed it
> to keep people from using native pointers, but the safety is not
> worth the loss of polymorphism.)
> 
> 
> 
> At 04:11 PM 6/13/00 -0400, Josh Cates wrote:
> >Hello developers,
> >
> >How do I up-cast or down-cast an Insight object?  Since we are using
> >smart pointers, I can't think of a way to use any of the conventional
> >methods such as dynamic_cast.
> >
> >For example, say I want to treat an itkImage::Pointer as an
> >itkObject::Pointer, etc,.
> >
> >In other words, how can I get polymorphic behavior from Insight objects?
> >
> >Thanks,
> >
> >Josh.
> >
> >+--+--+--+--+--+--+--+--+--+--+--+--
> >  Josh Cates                     
> >  Dept. of Electrical Engineering        
> >  University of Tennessee, Knoxville
> >  Email: jecates at utk.edu
> >  Phone: (865) 974-0694
> >  URL:   www.cs.utk.edu/~cates
> >--+--+--+--+--+--+--+--+--+--+--+---
> >
> >
> >_______________________________________________
> >Insight-developers mailing list
> >Insight-developers at public.kitware.com
> >http://public.kitware.com/mailman/listinfo/insight-developers 
> 
> 
> _______________________________________________
> Insight-developers mailing list
> Insight-developers at public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-developers
> 





More information about the Insight-developers mailing list