[vtk-developers] copy constructor and operator= revisited

Brad King brad.king at kitware.com
Thu Oct 11 10:17:34 EDT 2001


Hello, all:

In the past, vtkObject and all its subclasses have had the following
lines:

protected:
  vtkFoo();   // Implemented in cxx if not trivial.
  ~vtkFoo();  // Implemented in cxx if not trivial.
  vtkFoo(const vtkFoo&) {}
  void operator=(const vtkFoo&) {}

Since the copy constructor and assignment operator are never called,
Ken checked in a change a few weeks ago to give a more realistic
measure of coverage by removing the useless implementation code of
these two methds, leaving these lines instead:

protected:
  vtkFoo();   // Implemented in cxx if not trivial.
  ~vtkFoo();  // Implemented in cxx if not trivial.
  vtkFoo(const vtkFoo&);
  void operator=(const vtkFoo&);

We recently realized that when a subclass is missing the declaration
for one of these methods, the compiler generates a call to the
superclass's version.  Since there is no implementation of the method
in the superclass, a link-time error is generated, with little
indication of the reason for the missing symbol.

Such problems will be easier to find if they occur at compile time.
An easy way to cause this behavior is to make the methods private, so
that the compiler's generated call to them in the subclass produces an
access violation.

I have just checked in a change to nearly every class currently in
VTK, changing them to this form:

class vtkFoo: public vtkBar
{
public:
 // public stuff
protected:
  vtkFoo();   // Implemented in cxx if not trivial.
  ~vtkFoo();  // Implemented in cxx if not trivial.

  // ... rest of protected stuff ...

private:
  vtkFoo(const vtkFoo&);  // Not implemented.
  void operator=(const vtkFoo&);  // Not implemented.
};

The comments about "Not implemented" are there to prevent a reader
from searching the .cxx file for the implementation of the methods.
Any new VTK class should use this format.  If anyone notices an
existing class that I missed converting (done with an emacs keyboard
macro), please make the change and check it in.

-Brad




More information about the vtk-developers mailing list