digitalmars.D.learn - Struct life time
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; } 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. -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
Dec 14 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.
Dec 14 2008
BCS wrote:Reply to Qian,Thanks BCS, I am not able to get my own code right now. I will post it tomorrow. -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.comHi 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.
Dec 14 2008
BCS wrote:Reply to Qian,Hi, it was my fault. My wrong code was: ======================================== public TimeOfDay* getTime() { char[] timestring = "10:00:00"; // for test FullDate fd; parseTime(timestring, fd); return *fd.val.time(); } ======================================== Today I have changed it to: ======================================== public TimeOfDay* getTime() { char[] timestring = "10:00:00"; // for test FullDate fd; parseTime(timestring, fd); TimeOfDay* ret = new TimdOfDay(); *ret = fd.val.time(); return ret; } ======================================== Now it works properly. -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.comHi 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.
Dec 15 2008