How to call a pointer to a C ++ function from C?

I was successfully able to use the TUI header from the PDcurses library in my C ++ project in a previous post here .

now in my class I include the C header:

#include "tui.h" 

tui is in C and has this definition for a menu:

 typedef struct { char *name; /* item label */ FUNC func; /* (pointer to) function */ char *desc; /* function description */ } menu; 

so in MainView.cpp I have:

 void sub0() { //domenu(SubMenu0); } void sub1() { //domenu(SubMenu1); } void MainView::showMainMenu() { menu MainMenu[] = { { "Users", sub0, "Manage Users" }, { "Accounts", sub1, "Manage Accounts" }, { "Items", sub1, "Manage Items" }, { "Staff", sub1, "Manage Staff" }, { "", (FUNC)0, "" } }; startmenu(MainMenu, "Library System 1.0"); } 

works as expected.

The problem is that I need to make calls on my class methods in sub0 () and sub1 ().

I tried defining C ++ methods for my class to try and replace sub0 and sub1 with:

 void MainView::sub0() { //domenu(SubMenu0); } void MainView::sub1() { //domenu(SubMenu1); } 

the compiler sets this error:

 error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'FUNC' None of the functions with this name in scope match the target type 

What is the best way to pass these pointers to C code and get rid of this error?

thanks

+7
c ++ c
source share
2 answers

C ++ class objects have a pointer to "this", which is a kind of invisibly passed as the first argument to a member function of the class. This means that you cannot use a non-stationary member function for a pointer to a C-style function that takes 0 arguments.

So, allow this, you have several options. I need more details before telling you who to go with.

  • Make the functions you want to use in static C code.
  • Use boost or tr1 or some other C ++ library that allows you to bind member functions using this pointer. With boost, it would look like this:

     { "Users", boost::bind(&MainView::sub0, this), "Manage Users" }, 
  • You may be able to modify your code to pass a reference to the class object along with these function pointers. If so, you can call your callbacks directly on the desired object. It seems that you are dealing with a window manager, and therefore you probably will not be able to make the necessary changes for this approach.

  • It is likely that you will end up doing some work related to storing a pointer to an object that you are using somewhere, and then writing small stub functions that access that pointer and then calling a member function on it. Some (I dare say, most) window managers allow you to include a pointer to arbitrary data with a callback for this purpose.
+5
source share

I do not think that it is possible to pass a non-stationary member function as a FUNC pointer. Because the compiler passes this pointer to the entire non-static member function.

You have 2 options:

  • Keep your sub0 and sub1 in the class and stat them
  • Move your sub0 and sub1 outside the class. And pass a pointer to your class (it's it) to them in case you need to access other member variables / functions in sub0 / sub1.
+2
source share

All Articles