C++ sqlite3 function pointer

Pages: 12
Your usage of == for "1" and argv[0] doesn't do what you think it's doing as argv[0] is of type char*. Also, there's no need to convert argv[0] to val. Just use strcmp()

1
2
3
4
5
6
7
8
9
10
int myClass::callback(void* data, int argc, char** argv, char**)
{
    return strcmp(argv[0], "1") == 0;
}

int myClass::callback_thunk(void* data, int argc, char** argv, char** coln)
{
    reinterpret_cast<myClass*>(data)->callback(NULL, argc, argv, coln);
    return strcmp(argv[0], "1") == 0;
}

Last edited on
So I made some changes as below and though it all works like charm now, I don't know what's really happening between two functions and generally what you did to make it work? would appreciate your explanation here.

Non-static member functions have an extra parameter called the implicit object parameter. The implicit object parameter is a reference to the class of which the function is a member. For example, if class A is defined as
struct A { void f() {}; };
Then the non-static member function A::f has an implicit object parameter of type A&. It's not in the declaration, but it's there anyway.

So, to call A::f you must provide an implicit object argument corresponding to the implicit object parameter. This is the part that goes to the left of the dot, for example my_a is the implicit object argument in my_a.f(). You access the implicit object through the this pointer.

Absent polymorphism, the non-static member function A::f is conceptually the same as a non-member function g defined as
void g(A& a) {};

So non-static member functions always have an extra parameter, and sqlite3 can't handle this.

Non-member functions and static member functions do not have an implicit object parameter, so they avoid the problem entirely.

If you don't need to access any class members within callback then it is useless and should be removed. To do so, replace callback with callback_thunk, make it a static member and remove the line starting with reinterpret_cast.
Last edited on
Topic archived. No new replies allowed.
Pages: 12