简体   繁体   English

如何从C调用C ++函数指针?

[英]How to call a C++ function pointer from C?

I was successfully to finally be able to use TUI header from PDcurses library in my C++ project in a previous post here . 我在上一篇文章中成功地最终能够在我的C ++项目中使用PDcurses库中的TUI头。

now in my class I am including the C header: 现在在我班上我包括C头:

#include "tui.h"

tui is in C and has this definition for menu: tui在C中并且具有菜单的这个定义:

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

so in MainView.cpp I have: 所以在MainView.cpp我有:

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");
}

this is working as expected. 这是按预期工作的。

The problem is that i need to make calls on my class methods in sub0() and sub1(). 问题是我需要在sub0()和sub1()中调用我的类方法。

I tried to define C++ methods for my class to try and replace sub0 and sub1 with: 我尝试为我的类定义C ++方法,尝试用以下代码替换sub0和sub1:

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

the compiler is throwing 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 of passing those function pointers to the C code and get rid of that error? 将这些函数指针传递给C代码并摆脱该错误的最佳方法是什么?

thanks 谢谢

C++ class objects have a "this" pointer, which is sort of invisibly passed as the first argument to a class member function. C ++类对象有一个“this”指针,它被无形地作为类成员函数的第一个参数传递。 That means you can't use a non-static member function for a C-style function pointer that takes 0 arguments. 这意味着您不能将非静态成员函数用于带有0个参数的C样式函数指针。

So solve this, you have a few choices. 所以解决这个问题,你有几个选择。 I'd need more detail before telling you which one to go with. 在告诉你哪一个之前我需要更多细节。

  • Make the functions you want to use in the C code static. 使您想要在C代码中使用的函数静态。
  • Use boost or tr1 or some other C++ library that allows binding member functions with their this pointer. 使用boost或tr1或其他一些允许使用this指针绑定成员函数的C ++库。 With boost, this would look like: 使用boost,这看起来像:

     { "Users", boost::bind(&MainView::sub0, this), "Manage Users" }, 
  • You might be able to modify your code to pass around a reference to the class object along with these function pointers. 您可以修改代码以传递对类对象的引用以及这些函数指针。 If so, you can then invoke your callbacks directly on the object you want. 如果是这样,您可以直接在所需对象上调用回调。 It looks like you're dealing with a window manager though, and so you probably won't be able to make the changes needed for this approach to work. 看起来你正在处理一个窗口管理器,所以你可能无法进行这种方法所需的更改。

  • Probably what you will end up doing is some sort of work around, involving storing the pointer to the object you're using somewhere and then writing small stub functions which access this pointer and then call a member function on it. 可能你最终会做的是某种类型的工作,包括将指针存储到你正在使用的对象,然后编写访问该指针的小存根函数,然后在其上调用成员函数。 Some (dare I say, most) window managers allow you to include a pointer to arbitrary data with the callback for this purpose. 一些(我敢说,大多数)窗口管理器允许您为此目的包含一个指向任意数据的指针。

I don't think it's possible to pass a non-static member function as a FUNC pointer. 我不认为可以将非静态成员函数作为FUNC指针传递。 As the compiler will pass a this pointer to all non-static member function. 由于编译器会将this指针传递给所有非静态成员函数。

You have 2 choices: 你有2个选择:

  1. Keep your sub0 and sub1 in class and make them static 将sub0和sub1保持在类中并使它们保持静态
  2. Move your sub0 and sub1 out of class. 将sub0和sub1移出课堂。 And pass a pointer to your class(aka this ) to them in the case you need to access other member variables/functions in sub0/sub1. 在需要访问sub0 / sub1中的其他成员变量/函数的情况下,将指针传递给您的类(也称为this )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM