[vtk-developers] vtkNew<>

Brad King brad.king at kitware.com
Fri Jan 29 14:14:54 EST 2010


David Gobbi wrote:
> I guess that I should really give an example:
> 
> #include "vtkObject.h"
> #include "vtkNew.h"
> 
> class vtkTransform;
> 
> class vtkMyClass: public vtkObject
> {
> ...
> 
> protected:
> 
>   vtkNew<vtkTransform> Transform;
> 
> };
> 
> I know that it looks a little odd, but the utility of this should be obvious.

This is a generalization of the previous use cases for vtkNew<>.

The reason such ideas pop up is because vtkNew<> approximately turns VTK
objects into statically allocated instances, but with different syntax
and with the option of sharing ownership.  It is useful anywhere that
one might normally use a normal object (local stack allocation, member
variables, etc.).

You don't actually need to worry about whether the definitions of the
vtkNew<> constructor and destructor are available to the headers.  The
compiler will only instantiate them in translation units that have
definitions of vtkMyClass::vtkMyClass() or vtkMyClass::~vtkMyClass(),
which is only the vtkMyClass.cxx file.

However, I think messing with long-established header file conventions
is more intrusive than changing the way we allocate local variables
in implementation files.

-Brad



More information about the vtk-developers mailing list