www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.learn - Compare struct against null

↑ ↓ ← Heinz <malagana15 yahoo.es> writes:
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.
Apr 27 2008
↑ ↓ downs <default_357-line yahoo.de> writes:
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
Apr 28 2008
↑ ↓ Heinz <malagana15 yahoo.es> writes:
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)
Apr 28 2008
→ "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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 =3D 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 =3D hello(true, ...); if (h.assigned) DoSomethingOrOther(); This may or may not be enough for you needs. -- Simen
Apr 28 2008
→ downs <default_357-line yahoo.de> writes:
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.

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
Apr 29 2008