[vtkusers] Templated class members as callbacks for vtkCommand?

Mark Asbach mark.asbach at post.rwth-aachen.de
Fri Apr 18 08:04:27 EDT 2003


Hi Parag,

> I’m trying to attach a templated member function as the callback for 
> an event in VTK.

>  I chose the second approach because the first won’t work for my 
> purposes, so my code looks something like this:

Both approaches are identical, it's just that you can avoid name 
clashes if you embed the callback into a class by making it a static 
member.

> static void HistogramInteractorProcessPickEvent

> Then inside the class, I do the following:
>     
> m_PickEventCommand->SetCallback(&HistogramInteractorProcessPickEvent);

> This works fine; however, what I’d really like to do is have the C 
> function above be templated over the type of the itk Image, so 
> something like:
> This generates an error during compilation under both MSVC6 and Linux 
> gcc3.2, something akin to:
> :\development\aks\Code\Visualization\aksHistogramInteractor.cxx(68) : 
> error C2664: 'SetCallback' : cannot convert parameter 1 from 'void 
> (class vtkObject *,unsigned long,void *,void *)' to 'void (__cdecl 
> *)(class vtkObject *,unsigned long,void *,void *)’

The compiler tells you what's the problem: C calling style is needed, 
but you did provide something different (namely C++ standard calling 
style). It's just your luck that the non-templated version works 
(because your compilers assume it's C linkage, probably because it sits 
in a different file ending in .c).

Just tell the compiler that your template uses C calling style by 
surrounding declaration and definition with

extern "C"
{
	template<whatever>
	static void HistogramInteractorProcessPickEvent (class vtkObject 
*,unsigned long,void *,void *);
}

See  "The C++ Programming Language", second edition, "9.2.5 Linkage and 
Pointers to Functions" if there're questions left (or ask again here).

Yours,

Mark




More information about the vtkusers mailing list