Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
April 28, 2008 Compare struct against null | ||||
---|---|---|---|---|
| ||||
Hi, I have a simple struct and i want to check if a variable has been asigned: struct hello { } ... hello h; ... if(!h) { do stuff; } When compiling i get erros about opCall not implemented and many others. What can i do? Thanks in advance. |
April 28, 2008 Re: Compare struct against null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | Heinz wrote:
> Hi,
>
> I have a simple struct and i want to check if a variable has been asigned:
>
> struct hello
> {
>
> }
> ...
> hello h;
> ...
> if(!h)
> {
> do stuff;
> }
>
> When compiling i get erros about opCall not implemented and many others. What can i do?
>
> Thanks in advance.
Structs are value types. Read the spec. Then use a pointer :)
hello* h;
if (!h) h = new hello;
--downs
|
April 29, 2008 Re: Compare struct against null | ||||
---|---|---|---|---|
| ||||
Posted in reply to downs | downs Wrote:
> Heinz wrote:
> > Hi,
> >
> > I have a simple struct and i want to check if a variable has been asigned:
> >
> > struct hello
> > {
> >
> > }
> > ...
> > hello h;
> > ...
> > if(!h)
> > {
> > do stuff;
> > }
> >
> > When compiling i get erros about opCall not implemented and many others. What can i do?
> >
> > Thanks in advance.
>
> 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)
|
April 29, 2008 Re: Compare struct against null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | On Tue, 29 Apr 2008 06:50:18 +0200, Heinz <malagana15@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
|
April 29, 2008 Re: Compare struct against null | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz | Heinz wrote:
> downs Wrote:
>
>> Heinz wrote:
>>> Hi,
>>>
>>> I have a simple struct and i want to check if a variable has been asigned:
>>>
>>> struct hello
>>> {
>>>
>>> }
>>> ...
>>> hello h;
>>> ...
>>> if(!h)
>>> {
>>> do stuff;
>>> }
>>>
>>> When compiling i get erros about opCall not implemented and many others. What can i do?
>>>
>>> Thanks in advance.
>> 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.
Structs are *value types*.
That means, unless _explicitly newed_ (or part of a struct that's explicitly newed), they _always_ live on the stack.
Ergo, if &h is null, your complete callstack is shot :)
--downs
|
Copyright © 1999-2021 by the D Language Foundation