简体   繁体   English

在C ++中使用SQLite:对象函数作为回调

[英]Using SQLite in C++: object function as a callback

So, I am working on a side-project to keep my c++ skills fresh (it has been many years since I have done work in c++). 因此,我正在开展一个侧面项目,以保持我的c ++技能新鲜(自从我用c ++完成工作已经很多年了)。 I am working on something where I will be using SQLite. 我正在开发一些我将使用SQLite的东西。 I have a wrapper around the SQLite code. 我有一个SQLite代码的包装器。 One of the things I am noticing is that SQLite uses c-style callback functions in its sqlite3_exec(...) function. 我注意到的一件事是SQLite在其sqlite3_exec(...)函数中使用了c风格的回调函数。

I would like to have the callback function be an object method, as I would like it to be able to modify object variables but am unsure of how to do this exactly. 我想让回调函数成为一个对象方法,因为我希望它能够修改对象变量,但我不确定如何准确地执行此操作。 I have checked other similar questions on stackoverflow but came away with nothing helpful. 我已经检查了stackoverflow上的其他类似问题,但没有任何帮助。

Here is how I am declaring my wrapper class: 这是我如何声明我的包装类:

class DBAdapter
{
private:
    sqlite3* db;
    int getUserRecords(std::string);
    std::vector<USER_RECORD> records;

    int callbackSel(void*, int , char**, char**);

public:
    DBAdapter();
    ~DBAdapter();

    int open(std::string);
    void close();

    int insertRecord();
    int deleteRecord();
    int getNumUserRecords();
};

Here is how I am trying to use the callback (callbackSel), from within getNumUserRecords: 以下是我在getNumUserRecords中尝试使用回调(callbackSel)的方法:

int DBAdapter::getUserRecords(std::string name)
{
    std::string sql = "SELECT" + name + " from USERS";
    char* ErrMsg;
    char* data;

    int retval = sqlite3_exec(db,sql.c_str(),this->callbackSel,data,&ErrMsg);
    return retval;
}

The error message I am getting is: 我得到的错误消息是:

 error: ‘int (* DBAdapter::callbackSel)(void*, int, char**, char**)’ is not a static member of ‘class DBAdapter’

My problem is, if I make this a static function, I won't be able to have access to my vector, records, right? 我的问题是,如果我将其设为静态函数,我将无法访问我的向量,记录,对吧? Is there any way around this? 有没有办法解决?

Is there any way around this? 有没有办法解决?

I don't know for API specifically, but usually c-style callbacks support to have user data passed from a void* pointer (I'd guess the 1st parameter of that signature you mention). 我不知道 API具体,但通常c风格的回调支持从void*指针传递用户数据(我猜你提到的那个签名的第一个参数)。 What one typically does is: 通常做的是:

  1. Declare a static class function to specify as callback function pointer 声明一个静态类函数,指定为回调函数指针
  2. Implement that function to cast the passed user data pointer to a pointer to your class instance and call a member function 实现该函数以将传递的用户数据指针强制转换为指向类实例的指针并调用成员函数
  3. Have a member function defined in the class that provides the implementation you want 在提供所需实现的类中定义成员函数
  4. Pass a pointer to your handling class instance, when you're going to register the static member function pointer with the C API 当您要使用C API注册静态成员函数指针时,将指针传递给您的处理类实例

I hope this points out the right direction. 我希望这指出了正确的方向。

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

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