Extensions to types loaded from a C library

Jeremy DeHaan dehaan.jeremiah at gmail.com
Fri Dec 14 22:26:24 PST 2012


Just thought I should give an update. After testing a little bit, 
I found out that it is not safe in some situations.

With the code looking like this:

struct sfVector2f
{
     float x;
     float y;
	
	this(float X, float Y)
	{
		x = X;
		y = Y;
	}
}


The following does not work right:

sfSprite* sprite;

sprite = sfSprite_create();

sfVector2f originalLocation = sfSprite_getPosition(sprite);

writeln("Original ", originalLocation.x, " ", originalLocation.y);

sfVector2f newPosition = sfVector2f(100,200);

sfSprite_setPosition(sprite, newPosition);

sfVector2f returnedPosition = sfSprite_getPosition(sprite);
	
writeln("Output ", returnedPosition.x, " ", returnedPosition.y);


And outputs the following:

Original 1.4013e-44 4.12947e-39
Output 0 6.04679e-39


But when changed back to:
struct sfVector2f
{
     float x;
     float y;
}

The above code works as expected:
Original 0 0
Output 100 200



Thus it appears that any extensions made to the types defined in 
a C library is not safe! Even one as trivial as a constructor. I 
didn't show the code or output(it was with some different tests), 
but I also got some run time errors when using the version of 
sfVector2f at the top which went away when I changed it back to 
the original code.


On a different note, this site could REAAAAALLLYY use some sort 
of way to format code.



More information about the Digitalmars-d-learn mailing list