Is it any easier to use a class in a c++ file?
I think with C++ classes you only have access to its virtual methods.
To be able to use memory allocated in C via a BlitzMax Type, you need to do some trickery. And you cant use all BlitzMax types, such as Strings/Objects/Arrays etc.
And this may brake if there are changes to the way BlitzMax handles types.
This is just one way of doing it, there are probably more.
test.bmx:
Import "test.c"
Extern "C"
Type TStruct
Field I:Int
Field F:Float
Field S:Byte Ptr
EndType
Function CreateStruct( struct:TStruct Var, i:Int, f:Float, s$z)
Function CreateStruct2:TStruct( i:Int, f:Float, s$z)
Function FreeStruct( struct:TStruct Var)
EndExtern
'Local struct:TStruct = CreateStruct2( 10, 1.5, "Hello World!")
Local struct:TStruct
CreateStruct( struct, 10, 1.5, "Hello World!")
Print "ptr: " + Int(Byte Ptr struct)
Print "i: " + struct.i
Print "f: " + struct.f
Print "s: " + String.FromCString(struct.S)
FreeStruct( struct)
Print "ptr: " + Int(Byte Ptr struct)
test.c:
typedef struct {
int i;
float f;
char* s;
} STRUCT;
void CreateStruct( STRUCT** out, int i, float f, char* s) {
STRUCT* sc = (STRUCT*)malloc( sizeof( STRUCT));
sc->i = i;
sc->f = f;
sc->s = s;
*out = (char*)sc - 4; // this is the important part!
}
STRUCT* CreateStruct2( int i, float f, char* s) {
STRUCT* sc = (STRUCT*)malloc( sizeof( STRUCT));
sc->i = i;
sc->f = f;
sc->s = s;
return (char*)sc - 4; // this is the important part!
}
void FreeStruct( STRUCT** sc) {
if( (*sc)->s) free( (*sc)->s); // make sure we free the string, if any
free( *sc);
*sc = (void*)0;
}