Skip to content

Instantly share code, notes, and snippets.

@mbianco
Created January 27, 2020 13:54
Show Gist options
  • Save mbianco/1b0a34f5c63d902c4147d20894d5ca6c to your computer and use it in GitHub Desktop.
Save mbianco/1b0a34f5c63d902c4147d20894d5ca6c to your computer and use it in GitHub Desktop.
example of C to C++ interface
#include <memory>
#include "header.hpp"
std::unique_ptr<my_class<int>> obj;
extern "C"
void init_data(int x) {
obj = std::make_unique<my_class<int>>(x);
}
extern "C"
int value() {
return (*obj)();
}
void init_data(int x);
int value();
template <typename T>
class my_class {
T m_data;
public:
my_class(T && x) : m_data{x} {}
my_class(T const& x) : m_data{x} {}
T const& operator()() const {return m_data;}
T & operator()() {return m_data;}
};
#include "stdio.h"
#include "c_interfaces.h"
int main() {
init_data(42);
printf("%d\n", value());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment