[vtk-developers] Consistent MakeObject

Bill Hoffman bill.hoffman at kitware.com
Fri Feb 1 11:57:59 EST 2002


In VTK MakeObject is used as the name of a method that via a virtual function
call will create an object of the same type as a given pointer.  The
availability of this method is sporadic in VTK.  I would like
to propose a more consistent approach to this method.  It can all be
done in the existing TypeMacro.  It would look like this:


#define vtkTypeMacro(thisClass,superclass) \
virtual vtkObject* MakeVTKObject() { return thisClass::New();} \
thisClass* Make##thisClass()                           \
{ return thisClass::SafeDownCast(this->MakeVTKObject(); }      \
...


The result would be that two methods would be added to each class in VTK:

1.  A virtual method MakeVTKObject that would return a pointer to a vtkObject* that
was of the same type as the pointer calling the method.

2.  A method called MakethisClass would return a copy of the pointer but
casted to thisClass.

An example use would be this:

...
vtkImageReader* reader = vtkBMPReader::New();
foo(reader);
...

void foo(vtkImageReader* reader)
{
   vtkImageReader* newreader = reader->MakevtkImageReader();
   // newreader now points to a vtkBMPReader 
....
}

Note: if all compilers conformed to the ANSI standard, there would be one 
virtual MakeObject that returned thisClass* as you are allowed to return
different types from a virtual function as long as they are sub-classes.
However, the current set of compilers supported by VTK does not support this
feature, so we have to have the two method approach.

The reason for not using MakeObject is that it would break the existing API
for VTK, MakeObject could be deprecated.

-Bill




More information about the vtk-developers mailing list