Instantiate C struct on heap

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 4 16:49:26 PDT 2016


On Thursday, 4 August 2016 at 21:02:59 UTC, TencoDK wrote:
> Hey,
>
> I'm using DerelictSFML2 + CSFML, I have stuck on instantiating 
> window.
>
> derelict/window.d (binding from C):
> struct sfWindow;
>
> my_file.d:
> sfWindow* sfmlWindow = null;
> sfmlWindow = new sfWindow; // ???
>
> This code snippet gives me:
> Error: struct derelict.sfml2.window.sfWindow unknown size
>
> How must I create stuctures from C bindings?

C structs can be created with new just like D structs as long as 
they have a definition. sfWindow is an 'opaque type', which, just 
like a forward reference in C or C++, can not be instantiated 
directly because they have no definition. The error gives you a 
good hint with 'unknown size'. Many C libraries work this way, 
hiding the implementation internally and exposing an opaque type 
in the public headers.


More information about the Digitalmars-d-learn mailing list