Gam*_*Ray 6 c c++ inheritance class wrapper
我只是想知道是否有办法为具有继承的c ++类创建ac包装器API.
考虑以下:
class sampleClass1 : public sampleClass{
public:
int get() { return this.data *2; };
void set(int data);
}
class sampleClass : public sample{
public:
int get() { return this.data; }
void set(int data) {this.data = data; }
}
class sample {
public:
virtual int get();
virtual void set(int data);
private:
int data;
}
Run Code Online (Sandbox Code Playgroud)
如何包装sampleClass1以使其在ac上下文中工作?
谢谢,
首先,你sample应该得到一个合适的virtualdtor.
接下来,只需为每个函数添加一个带有C绑定的自由函数,该函数是接口的一部分,只需委托:
#ifdef __cplusplus
extern "C" {
#endif
typedef struct sample sample;
sample* sample_create();
sample* sample_create0();
sample* sample_create1();
void sample_destroy(sample*);
int sample_get(sample*);
void sample_set(sample*, int);
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
#include "sample.h" // Included first to find errors
#include "sample.hpp" // complete the types and get the public interface
sample* sample_create() {return new sample;}
sample* sample_create0() {return new sampleClass;}
sample* sample_create1() {return new sampleClass1;}
void sample_destroy(sample* p) {delete p;}
int sample_get(sample* p) {return p->get();}
void sample_set(sample* p, int x) {p->set(x);
Run Code Online (Sandbox Code Playgroud)
// Your C++ header here, with class definition
Run Code Online (Sandbox Code Playgroud)
#include "sample.hpp" // Included first to find errors
// Implement the class here
Run Code Online (Sandbox Code Playgroud)