Hey--
I'm trying to wrap a third party C library in a C++ wrapper, and I'm trying to do it as efficiently as possible. The bulk of the library revolves around a core data structure, which I'll simplify as:
I'd like to wrap that in a class, but I don't want to create a class that contains a Data, I'd like to subclass Data (struct being a special case of class):
The reason for this is that it allows me to use my class in place of the legacy data structure in existing functions.
The way I'm doing that above requires allocating storage, copying the contents of the Data object I'm wrapping, and then deallocating that object. Is there a way to just use the existing object and rewrap it?
"static_cast<OOData*>d" comes to mind, but that bypasses any constructors of OOData and therefore doesn't allow additional initialization.
Any ideas?
I'm trying to wrap a third party C library in a C++ wrapper, and I'm trying to do it as efficiently as possible. The bulk of the library revolves around a core data structure, which I'll simplify as:
Code:
typedef struct Data
{
int x;
int bigArrayType;
char * bigArray;
} Data;
I'd like to wrap that in a class, but I don't want to create a class that contains a Data, I'd like to subclass Data (struct being a special case of class):
Code:
class OOData: public Data
{
private:
inline void initFromDataPtr(Data* d){*(Data*)this=*d;};
public:
OOData(Data* d){initFromDataPtr(d);};
};
The reason for this is that it allows me to use my class in place of the legacy data structure in existing functions.
The way I'm doing that above requires allocating storage, copying the contents of the Data object I'm wrapping, and then deallocating that object. Is there a way to just use the existing object and rewrap it?
"static_cast<OOData*>d" comes to mind, but that bypasses any constructors of OOData and therefore doesn't allow additional initialization.
Any ideas?