Compare struct against null

Simen Kjaeraas simen.kjaras at gmail.com
Mon Apr 28 23:19:55 PDT 2008


On Tue, 29 Apr 2008 06:50:18 +0200, Heinz <malagana15 at yahoo.es> wrote:

> downs Wrote:
>> Structs are value types. Read the spec. Then use a pointer :)
>>
>> hello* h;
>>
>> if (!h) h = new hello;
>>
>>  --downs
>
> Thanks for your reply. Can i use then?:
>  hello h;
> if(!&h)


No. &h will always return a non-null pointer (h exists, in other words).

You might use some convoluted workaround to make hello aware of whether or
not it has been assigned something.

Easy example:

struct hello
{
   bool assigned;
   // other fields;
}

hello h;

h = hello(true, ...);

if (h.assigned)
   DoSomethingOrOther();


This may or may not be enough for you needs.

-- Simen


More information about the Digitalmars-d-learn mailing list