# vtkCallbackCommand for class member functions

**URL:** https://discourse.vtk.org/t/vtkcallbackcommand-for-class-member-functions/6591
**Category:** Support
**Created:** [September 9, 2021, 11:30am UTC](https://discourse.vtk.org/t/vtkcallbackcommand-for-class-member-functions/6591 "2021-09-09T11:30:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![PWeissenbacher](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pweissenbacher/32/3686_2.png) [@PWeissenbacher](https://discourse.vtk.org/u/PWeissenbacher)
#### Post date: [September 9, 2021, 11:30am UTC](https://discourse.vtk.org/t/vtkcallbackcommand-for-class-member-functions/6591/1 "2021-09-09T11:30:32Z")

</div>

Hello VTK Community.

I’m looking for a way, to register a class member function as a callback to a `vtkCallbackCommand` like that:

```auto
class foo
{
public:
   void bar(vtkObject* caller, unsigned long eId, void* clientData, void* callerData);
}

Foo::Foo()
{
   auto callbackCommand = vtkSmartPointer<vtkCallbackCommand>::New();
   callbackCommand->SetCallback(bar);
   this->AddObserver(vtkCommand::<event-name>, callbackCommand);
}

```

Using `vtkCallbackCommand` I get an error like:

`argument of type void (foo::*)(vtkObject *, unsigned long, void *, void *) is incompatible with parameter of type void (*)(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata)`

It seems that such functionality is already implemented within Paraview (?), see: [ParaView: vtkMemberFunctionCommand\< ClassT \> Class Template Reference](https://kitware.github.io/paraview-docs/latest/cxx/classvtkMemberFunctionCommand.html)

Is there already a similar implementation in VTK C++ which I’m missing?

---

<div class="post-metadata">

### Author: ![nicfel](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicfel/32/3639_2.png) [@nicfel](https://discourse.vtk.org/u/nicfel)
#### Post date: [September 9, 2021, 12:04pm UTC](https://discourse.vtk.org/t/vtkcallbackcommand-for-class-member-functions/6591/2 "2021-09-09T12:04:20Z")

</div>

I believe the issue here is that the bar function needs an object to be invoked on. If you change the bar function such that it is a static function it should work. If you need bar to be a non static function you can pass the object it should be invoked on via the clientData pointer. Here is an explanation for that: [https://vtk.org/Wiki/VTK/Tutorials/Callbacks](https://vtk.org/Wiki/VTK/Tutorials/Callbacks)

If you are using Qt you can have a look at this: [vtkEventQtSlotConnect: Slot not being called - #2 by nicfel](https://discourse.vtk.org/t/vtkeventqtslotconnect-slot-not-being-called/6566/2) and the example [https://kitware.github.io/vtk-examples/site/Cxx/Qt/EventQtSlotConnect/](https://kitware.github.io/vtk-examples/site/Cxx/Qt/EventQtSlotConnect/)

---

<div class="post-metadata">

### Author: ![PWeissenbacher](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pweissenbacher/32/3686_2.png) [@PWeissenbacher](https://discourse.vtk.org/u/PWeissenbacher)
#### Post date: [September 9, 2021, 2:39pm UTC](https://discourse.vtk.org/t/vtkcallbackcommand-for-class-member-functions/6591/3 "2021-09-09T14:39:49Z")

</div>

Hi @nicfel

Ok, that’s what I thought so too. But i indeed need a non-static function to be the callback.

The `clientData` is the way to go here I think … I hoped there would be a “cleaner” way already implemented in VTK, as ParaView seems to have that functionality.

Thanks, for your answer!

Best Regards  
Philipp

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [September 9, 2021, 4:37pm UTC](https://discourse.vtk.org/t/vtkcallbackcommand-for-class-member-functions/6591/4 "2021-09-09T16:37:39Z")

</div>

You can register an observer callback method directly with vtkObject (see the documentation for [vtkObject::SetObserver](https://vtk.org/doc/release/7.1/html/classvtkObject.html#a35472ff2c15623e8adcd7a9f8e6370f4)).

When used this way, the callback has no clientData (just callerData):

```auto
void foo::bar(vtkObject* caller, unsigned long eId, void* callerData);

```

---

<div class="post-metadata">

### Author: ![PWeissenbacher](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pweissenbacher/32/3686_2.png) [@PWeissenbacher](https://discourse.vtk.org/u/PWeissenbacher)
#### Post date: [September 10, 2021, 8:24am UTC](https://discourse.vtk.org/t/vtkcallbackcommand-for-class-member-functions/6591/5 "2021-09-10T08:24:15Z")

</div>

Perfect - that’s what I was looking for.

So when using something like this:

```auto
foo:foo()
{
   foo->AddObserver(vtkCommand::<event-name>, this, &foo::bar);
}

```

The callback is able to run the member function, which enables me to use member variables aswell, without setting `clientData` of the `AddObserver()` call from my initial post.

Thank you @dgobbi

Best Regards  
Phlipp
