Class Default Value

Derek Parnell derek at psych.ward
Tue Apr 25 03:29:48 PDT 2006


On Tue, 25 Apr 2006 13:19:02 +1000, Alberto Simon  
<Alberto_member at 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



More information about the Digitalmars-d mailing list