[Insight-users] problems with templates and virtual functions

Samson Timoner samson at bwh.harvard.edu
Fri, 23 Apr 2004 01:30:41 -0400


I've run into a problem with virtual functions and templates and itk.
(I'm using visual studio7.0 and Windows XP). The problem is that one
of my virtual functions in the virtual function table does not seem to
be set, so that when I call the function, my program crashes.

I have reduced the problem to the following situation of 3 classes: a
base class, a templated child class with a virtual function, and then
a child class of that that template. For example:

class Object {
 virtual void PrintSelf();
}

template<class T>
class TemplateClass() : public Object {
  virtual void Update();
}

class DoSomething() : public TemplateClass<int> {
 void PrintSelf();
 void Update();
}

 I start my program in a debugger and instantiate an example of
"DoSomething". I can then examine the virtual function table. For some
reason, "Update" is not set correctly in the virtual function table.
Oddly enough, "PrintSelf" is set correctly.

I currently have the class "DoSomething" sitting in my own library
(with the appropriate __decspec(dllexport) and dllimport). Moving it
out of the library and linking the code directly into my apprications
seems to solve the problem, but I do not know why. And, I'd really
like to make the library.

Has anyone seen this kind of problem with virtual functions and templates and libaries before? Is there compiler flag that might
fix the problem?

Is there an example class in one of the ITK libraries, maybe an
ImageToImageFilter that only works on one type of image, or an image
importer that only works with one type of image?

-- Samson