[Insight-developers] proper way to test for a superclass?

Luis Ibanez luis.ibanez at kitware.com
Sat Oct 23 16:52:34 EDT 2010


Hi Michael,

Thanks for pointing this out.

The code in the Test is certainly unnecessarily convoluted.

This code has now been corrected and pushed to Gerrit:
http://review.source.kitware.com/#change,227

See the diff here:

http://review.source.kitware.com/#patch,sidebyside,227,1,Testing/Code/Review/itkTransformFileReaderWriterTest.cxx

Essentially:

Instead of


AffineTransformPointer affine_read = static_cast<AffineTransformType*>((*tit
).GetPointer());

affine_transform1 = dynamic_cast< AffineTransformType * >( affine_read.
GetPointer() );



We should do:


TransformPointer transform_read = tit->GetPointer();
affine_transform1 = dynamic_cast< AffineTransformType * >( transform_read.
GetPointer() );



Please let us know if you have further questions,


     Thanks


            Luis


--------------------------------------------
On Fri, Oct 22, 2010 at 7:49 PM, Michael Stauffer (Circular Logic) <
mstauff at verizon.net> wrote:

>  Luis,
>
> Thanks for the example code. Do you know why it uses a static_cast first in
> the code? Is it just for style/readability, or something else I'm missing?
>
> itkTransformFileReaderWriterTest.cxx:
>
>     AffineTransformPointer affine_read =
> static_cast<AffineTransformType*>((*tit).GetPointer());
>     affine_transform1 = dynamic_cast< AffineTransformType * >(
> affine_read.GetPointer() );
> I'm doing just this, based on some code I saw elsewhere. It seems to work:
>
>     typename TransformType::ConstPointer transform;
>     transform = dynamic_cast<TransformType *>( ( (
> transformReader->GetTransformList() )->front() ).GetPointer() );
> Cheers,
> Michael
>
>  ------------------------------
> *From:* Luis Ibanez [mailto:luis.ibanez at kitware.com]
> *Sent:* Saturday, October 16, 2010 3:00 PM
>
> *To:* Michael Stauffer (Circular Logic)
> *Cc:* ITK-dev-list
> *Subject:* Re: [Insight-developers] proper way to test for a superclass?
>
>
> Hi Michael,
>
> An attempt to dynamic_cast, followed by checking
> whether the returned pointer is null or not, is the
> standard way in which we check for the actual
> type of a class.
>
> Please note that IO is probably the only space in
> which dynamic_casting can be tolerated as a
> standard practice.
>
>
> In general, no dynamic_cast should be needed
> in a well designed generic programming package.
>
>
> You may want to look at the code in
>
> ITK/Testing/Code/Review/itkTransformFileReaderWriterTest.cxx
>
>
>
> B B B Best,
>
>
>
> B B B B B B Luis
>
>
> -------------------------------------------------------------
> On Wed, Oct 13, 2010 at 11:23 AM, Michael Stauffer (Circular Logic) <
> mstauff at verizon.net> wrote:
>
>>  Hi Luis,
>> B
>> Yes sorry, that wasn't clear.
>> B
>> For example, I'd like to test at runtime that an object is a
>> MatrixOffsetTransformBase class, or a subclass thereof, to make sure it has
>> certain member functions. The issue comes from reading a transform file
>> using the transform file reader, and wanting to make sure the output object
>> is of the type or a subclass.
>> B
>> I can't see in the TransformFileReader class how I can tell or controlB
>> what kind of transform type it's returning, except for trying a dynamic
>> class. Am I missing some way to do so more properly? The documentation
>> doesn't seem to cover this.
>> B
>> Cheers,
>> Michael
>>
>>  ------------------------------
>> *From:* Luis Ibanez [mailto:luis.ibanez at kitware.com]
>> *Sent:* Wednesday, October 13, 2010 9:50 AM
>> *To:* Michael Stauffer (Circular Logic)
>> *Cc:* ITK-dev-list
>> *Subject:* Re: [Insight-developers] proper way to test for a superclass?
>>
>>
>> Hi Michael,
>>
>> What do you mean by a "minimum superclass" ?
>>
>> Please explain.
>>
>> B B Thanks
>>
>>
>> B B B B B B B B Luis
>>
>>
>>
>> ---------------------------------------------------
>> On Tue, Oct 12, 2010 at 4:51 PM, Michael Stauffer (Circular Logic) <
>> mstauff at verizon.net> wrote:
>>
>>> Hi,
>>>
>>> What's the proper way in ITK to test for a minimum superclass? I could
>>> do a dynamic cast and check for NULL - is that acceptable? Thanks.
>>>
>>> Cheers,
>>> Michael
>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20101023/a0cbf65a/attachment.htm>


More information about the Insight-developers mailing list