I want to write simple C++ wrapping (for RAII purposes) around the following C API:
typedef void* T;
T createT(Arg arg);
int foo(T t);
closeT(T t);
int bar(const T* ptrToArrayOfT, unsigned long size);
For the first three functions it's simple (for shortiness I omit checking for errors and throwing exceptions):
class C {
public:
C(Arg arg) : t_(createT(arg)) {}
~C() { closeT(t_); }
int doFoo() { return foo(t_); }
private:
T t_;
}
As for bar
I would like to have function with the following signature:
int doBar(const vector<C>& vec);
Here is how I'm thinking of implementing it:
int doBar(const vector<C>& vec) {
static_assert(sizeof(C) == sizeof(T));
return bar(reinterpret_cast<const T*>(vec.data()), vec.size());
}
Is it safe way to do? (I doubt because C
has private member). If it's not, then is there any way to implement bar()
without giving out t_
member of C
? (Using vector is not necessary).