[vtkusers] Just plain wrong
Ken Martin
ken.martin at kitware.com
Wed Dec 5 21:46:52 EST 2001
Hello Adam,
The code in question was written before dynamic_cast existed (cfront). Many
older compilers do not provide dynamic cast support as the default even
today though they should. The solution right now would be:
vtkStructuredGrid* sg = vtkStructuredGrid::SafeDownCast( ds );
if ( 0 == ds )
{
vtkErrorMacro( << "Type mismatch in CopyStructure." );
}
You will find old style typecasts in a number of places in VTK although they
are being replaced with static_cast and reinterpret_cast typically when we
modify the code. We do not use dynamic_cast yet due to compatibility issues
for some people. Instead we provide the SafeDownCast method. Basically a
poor mans RTTI.
Thanks
Ken
----- Original Message -----
From: "Adam D. Burry" <aburry at igotechnologies.com>
To: <vtkusers at public.kitware.com>
Sent: Wednesday, December 05, 2001 9:17 PM
Subject: [vtkusers] Just plain wrong
> Hi:
>
> This code from vtk-3.2 is just plain wrong. There is an argument on
> technical grounds that it is ok, but morally: it is always wrong:
>
> void vtkStructuredGrid::CopyStructure(vtkDataSet *ds)
> {
> vtkStructuredGrid *sg=(vtkStructuredGrid *)ds;
> // ...
>
> At the very least it would be better written as:
>
> void vtkStructuredGrid::CopyStructure(vtkDataSet *ds)
> {
> vtkStructuredGrid* sg = dynamic_cast<vtkStructuredGrid*>( ds );
> if ( 0 == ds )
> {
> vtkErrorMacro( << "Type mismatch in CopyStructure." );
> }
> // ...
>
> Even better, assuming this method must be different from an assignment
> operator or a copy constructor, would be:
>
> void vtkStructuredGrid::CopyStructure(vtkStructuredGrid* ds)
> {
> // ...
>
> The problem, if it is not clear from the code, is that the vtk-3.2 code
> is forcing a downcast that is not necessarily safe. So if you are using
> CopyStructure(), be careful.
>
> Adam
>
> --
> Mr. Adam D. Burry, Software Developer
> aburry at igotechnologies.com
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at:
<http://public.kitware.com/cgi-bin/vtkfaq>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
More information about the vtkusers
mailing list