Struct life time

BCS ao at pathlink.com
Sun Dec 14 12:03:29 PST 2008


Reply to Qian,

> Hi All,
> 
> I am new to D community. I have some problems with TimeOfDay struct.
> 
> I have defined a class:
> ========================================================
> class Test
> {
> TimeOfDay* getTime() {
> return a_valid_timeofday_pointer;
> }

Could you expand on this function? The rest looks good but if this function 
is returning a pointer to something that then goes out of scope (a local 
var for instance) than that would be the bug.

> void setTime(TimeOfDay* value) {
> setValueAsTime(*value);
> }
> void setValueAsTime(TimeOfDay value) {
> Cout(TimeToChar(value)).newline;
> }
> /* the rest implementation of this class ... */
> }
> ========================================================
> I try to test if setTime() works properly.
> 
> If I call
> Cout(TimeToChar(*TestObj.getTime())).newline;
> It works. The console will show "10:00:00" for example.
> If I call
> TestObj.setTime(TestObj.getTime());
> It does not work. The console will show "33140305:48698544:485947594"
> for example.
> 
> If I change the setTime() function to
> void setTime(TimeOfDay* value) {
> Cout(TimeToChar(*value)).newline;
> }
> or
> void setTime(TimeOfDay* value) {
> TimeOfDay copy = *value;
> setValueAsTime(copy);
> }
> It works.
> What's wrong with my code? (BTW: I do not want to change TimeOfDay* to
> TimeOfDay, otherwise null cannot be passed in.)
> 
> Thanks in advance.
> 




More information about the Digitalmars-d-learn mailing list