SysTime in a Struct

Ali Çehreli acehreli at yahoo.com
Thu Mar 1 09:37:22 PST 2012


On 03/01/2012 09:14 AM, albatroz wrote:
 > Have fixed the segfault by using DateTime instead of SysTime.
 >>
 >> That is a separate local variable within this(this). Also, this(this)
 >> is the postblit (similar to a copy constructor). Is that what you want
 >> to define?
 >
 > No, but not using this(this) will fail to build with:
 > static variable _initialized cannot be read at compile time

So you are trying to initialize the member with a default initializer, 
like this:

struct prevEv {
// ...
   DateTime edatetime = DateTime(/* ... */);
}

For that to work, the initial value must be a compile-time value. I am 
not sure that you want that.

Also, this(this) is needed in very rare cases. Are you trying to create 
objects of prevEv? Then you should use this(). Unfortunately, structs 
cannot have default constructors.

A solution might be to use a function that makes and returns a prevEv:

prevEv make_prevEv()
{
     return prevEv(/* ... */, DateTime(/* ... */));
}

 > Making the struct similar to what you suggest will work.
 > struct prevEv {
 > string edate;
 > string etime;
 > string etext;
 > DateTime edatetime;
 > this (this)
 > {
 > edatetime = DateTime(
 > Clock.currTime.year,
 > to!int(this.edate[2..4]),
 > to!int(this.edate[0..2]),
 > to!int(etime[0..2]),
 > to!int(etime[3..5]),
 > to!int(etime[6..8]));
 > }
 > }
 >
 > Just not has expected, do you have any suggestion how to do it properly?

Are you trying to record the time when a prevEv is copied from another 
one? If not, I suggest not defining this(this). It is the postblit, to 
make things right for rare structs and only when the compiler generated 
copying is wrong for a that type.

Or, are you just trying to define a type that contains time information?

 >
 > writeln ( prevEv.edatetime ); //0001-Jan-01 00:00:00
 > writeln ( prevEv ); // preEv("140212", "05:13:26", "9 140212 05:13:26
 > d", 2012-Feb-14 05:13:26)
 >
 >>
 >>
 >> Ali
 > Thanks
 >

Ali



More information about the Digitalmars-d-learn mailing list