[Insight-developers] Code Headscratcher for the day: GetValueAndDerivative

Brad King brad.king at kitware.com
Thu Dec 2 17:04:17 EST 2010


On 12/02/2010 04:35 PM, kent williams wrote:
> But this is the first time I've seen a class redefine an implemented virtual
> function in the parent as pure virtual.

Strange.

> And in this case, I don't know why it does so. How many ways are there to
> implement GetValueAndDerivative?

For some cost functions it is possible to share computation of
terms that appear both in the value and in its derivative.  In
those functions this method can be implemented as an optimization
over just GetValue and GetDerivative.

> And while I'm on the subject of headscratchers -- why does
> itk::ImageToImageMetric define some methods as virtual inline?  In-line
> means expand the function body in-line where its called, and virtual means
> call the function via a pointer in the object's VTable.  How do you do both?

Virtual methods can be called directly without a vtable lookup by
naming them explicitly.  For example:

  struct A {
    inline virtual int f(int x) { return x+1; }
  };
  struct B: public A {
    int f(int x) { return this->A::f(x)+1; }
  };

The call to "A::f" does not use the vtable so the body of the function
can be inlined inside the "B::f" implementation.  That way the code

  ptr->f(1);

does 1 vtable lookup and calls B::f whose implementation has been
optimized to just

  return x+2;

by the compiler.  The optimization removes an extra function call.

-Brad


More information about the Insight-developers mailing list