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

David Doria daviddoria at gmail.com
Tue Feb 8 08:19:06 EST 2011


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110208/8bb56042/attachment.htm>


More information about the Insight-users mailing list