Created
January 27, 2020 13:54
-
-
Save mbianco/1b0a34f5c63d902c4147d20894d5ca6c to your computer and use it in GitHub Desktop.
example of C to C++ interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void init_data(int x); | |
int value(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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