[vtkusers] Templated class members as callbacks for vtkCommand?

Parag Chandra pchandra at radonc.unc.edu
Fri Apr 18 10:21:56 EDT 2003


Thanks Mark. Though after posting the message, I started digging through
the code for vtkImageViewer and followed its example of deriving a
separate, templated vtkCommand class to encapsulate the callback. This
works perfectly, and more closely mimics the ITK style that I am used
to.

Best regards,
Parag Chandra

-----Original Message-----
From: Mark Asbach [mailto:mark.asbach at post.rwth-aachen.de] 
Sent: Friday, April 18, 2003 7:04 AM
To: Parag Chandra
Cc: vtkusers at public.kitware.com
Subject: Re: [vtkusers] Templated class members as callbacks for
vtkCommand?

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