www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Class Default Value

reply Alberto Simon <Alberto_member pathlink.com> writes:
I have a question... When I declare a class object no matter what the definition
is and set it to null I get an runtime exception whenever I try to compare that
object to null... Is that correct behavior?? and if that is correct, how do I
check for an uninitialized class in a fast and efficient manner?

Regards,
Alberto Simon
Apr 24 2006
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Alberto Simon" <Alberto_member pathlink.com> wrote in message 
news:e2k4f5$1efa$1 digitaldaemon.com...
I have a question... When I declare a class object no matter what the 
definition
 is and set it to null I get an runtime exception whenever I try to compare 
 that
 object to null... Is that correct behavior?? and if that is correct, how 
 do I
 check for an uninitialized class in a fast and efficient manner?
Use "is", not "==".
Apr 24 2006
prev sibling parent "Derek Parnell" <derek psych.ward> writes:
On Tue, 25 Apr 2006 13:19:02 +1000, Alberto Simon  
<Alberto_member pathlink.com> wrote:

 I have a question... When I declare a class object no matter what the  
 definition
 is and set it to null I get an runtime exception whenever I try to  
 compare that
 object to null... Is that correct behavior?? and if that is correct, how  
 do I
 check for an uninitialized class in a fast and efficient manner?
When one codes ... class Foo {}; Foo foo = new Foo; ... if (foo == null) the compile converts this to call the implied class member function 'opEquals' so this code is equivalent to if (foo.opEquals(null)) and for that to work, 'foo' must be a valid class instance. The way to check for an uninitialized class in D is to let the compiler know that's what you are trying to do, and the syntax for that is ... if (foo is null) To test if it is initialized use if(foo !is null) or if (! (foo is null) ) -- Derek Parnell Melbourne, Australia
Apr 25 2006