[Insight-users] Returning a value of the same same type as a class template

Dan Mueller dan.muel at gmail.com
Tue Feb 8 09:19:08 EST 2011


Hi David,

Simple question: is there a reason you can't template Form over T?
   http://codepad.org/SzExS82y

Then you could instantiate different forms based on the user specified type...

Cheers, Dan

On 8 February 2011 14:19, David Doria <daviddoria at gmail.com> wrote:
> I frequently have this situation:
> - A Qt form object called Form.
> - I want to store an object of a class which encapsulates a particular
> functionality, say ImageSegmentation in the Form object
> - ImageSegmentation is templated on the type of image it should operate on,
> and this type is specified by a user action at runtime
> - Because of this, I can't store a Segmentation<T> as a member variable in
> Form because I don't know T.
> - I've been overcoming this by creating a non-templated SegmentationBase
> class that I can store a pointer to in Form (i.e. SegmentationBase*
> MySegmentationBase;
> - I then instantiate the class at runtime with
> MySegmentationBase = new Segmentation<itk::Image<unsigned char, 2> >();
> This has been working, except that I end up jumping through hoops to get the
> output of the segmentation (or whatever operation I am doing with this
> pattern). That is, I want to have a
> template <typename T>
> class Segmentation : public SegmentationBase
> {
>  T GetResult();
> }
> that I can call from:
> Form::SomeFunction()
> {
>  T result = MyImageSegmentationBase->GetResult();
>  ... do something with 'result' ...
> }
> The problem with that is two fold.
> 1) GetResult is only defined in Segmentation (not the Base) because the
> return type is T.
> 2) I don't know T in SomeFunction(), so I can't get the value anyway.
> I tried to fix (1) by making a pure virtual GetImage() in SegmentationBase
> which returns a itk::DataObject::Pointer and also making Segmentation return
> a itk::DataObject::Pointer, but it has trouble with the conversion from
> T::Pointer to itk::DataObject::Pointer. But even if that worked, I'd be
> stuck with (2).
> Here is the whole layout:
> http://codepad.org/lWvNYc2g
> Any suggestions? I hope I am just making this more complicated than it needs
> to be, because it seems like a pretty standard thing to want to do!
> Thanks,
>
> David
>
> _____________________________________
> 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://www.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-users
>
>


More information about the Insight-users mailing list