www.digitalmars.com         C & C++   DMDScript  

D - Struct constructors

reply Simon Hobbs <Simon_member pathlink.com> writes:
Just started looking at D (from a game programming perspective) and it seems
superb in almost every way, except it doesn't appear that structs can have
constructors. Is this correct?

If so, how am I to implement a usable Vector4 type (i.e. an array of 4 floats)?
And please don't tell me to make it a class - the overhead is not acceptable -
this has to be a value type.

Here's hoping I'm missing something.

Si
Apr 15 2004
parent Andy Friesen <andy ikagames.com> writes:
Simon Hobbs wrote:
 Just started looking at D (from a game programming perspective) and it seems
 superb in almost every way, except it doesn't appear that structs can have
 constructors. Is this correct?
 
 If so, how am I to implement a usable Vector4 type (i.e. an array of 4 floats)?
 And please don't tell me to make it a class - the overhead is not acceptable -
 this has to be a value type.
 
 Here's hoping I'm missing something.
You can override operator() and make it static like so: struct Vec4 { float w,x,y,z; static Vec4 opCall(float W, float X, float Y, float Z) { ... } } Vec4 blah = Vec4(0,0,0,1); -- andy
Apr 15 2004