0

The openssl library provides a way to write your own BIOs. Essentially you load a struct of function pointers to read, write, create, destroy, etc, routines that the openssl lib will use.

I am trying of wrap this functionality in a C++ class. openssl has routines to set the function pointers of the structure it uses, like:

BIO_meth_set_read(pBIOMethod, my_function_for_read); 

Question: can I use a class method for my_function_for_read? In other words, can the address of a class method be invoked as a regular function in an external library that is C-based?

I am not sure if the class vtable is used if I take the address of just that class method.

Thanks, -Andres

Andres Gonzalez
  • 2,129
  • 5
  • 29
  • 43
  • Do you want to call the method of a class in C library? If so, you can't as C is not aware of class instance. But you could be able to call static method of the class. Anyway, check this technique http://stackoverflow.com/questions/14815274/how-to-call-a-c-method-from-c – Dom Jul 22 '16 at 21:04

1 Answers1

0

You can call static member functions, which you can wrap in simple C functions. For example:

a.cc:

#include <stdio.h>

class C {
public:
  static void hello();
};

void C::hello()
{
  printf("hello\n");
}

extern "C" {
  void hi()
  {
    C::hello();
  }
}

b.c:

extern void hi();

int main()
{
  hi();
}

then:

g++ -c -o a.o a.cc
gcc b.c a.o
./a.out
hello

In the static member function, you can do any C++ you like. For example, cast an argument to a pointer to an instance of C and then call a member function, etc.

Jim Flood
  • 8,144
  • 3
  • 36
  • 48