C++ question about returning null

Started by
17 comments, last by d000hg 18 years, 9 months ago
Quote:Original post by d000hg
Yeah, technically you should have "NULL" and not "0", althugh I think "0" is nicer to look at.


Well technicall NULL is 0
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Advertisement
Quote:Original post by furby100
Quote:Original post by d000hg
Yes that's what you want.
CBullet *CreateBulletIfFire(bool isFiring){  if(!isFiring)    return NULL;  else    return new CBullet;}


Warning: Not all control paths return a value.

Remove the else and you'll be fine.


Odd.. both control paths DO return a value. In both logical ways that comparison is evaluated, each results in a value returned. Should be no problem there..

	int *bleh(bool argh)	{		if (argh)			return NULL;		else			return new int;	}


Compiles fine under MSVC.NET, and it's the same code (substituting a primitive for an object)
Quote:Original post by furby100
Quote:Original post by d000hg
Yes that's what you want.
CBullet *CreateBulletIfFire(bool isFiring)
{
if(!isFiring)
return NULL;
else
return new CBullet;
}


Warning: Not all control paths return a value.

Remove the else and you'll be fine.


Incorrect. All control paths -DO- return a value... it's an all-inclusive set of boolean comparisons.
My compiler doesn't like it.
Quote:Original post by furby100
My compiler doesn't like it.

That'd be the fault of your compiler, not the code.
Quote:Original post by furby100
Quote:Original post by d000hg
Yes that's what you want.
CBullet *CreateBulletIfFire(bool isFiring){  if(!isFiring)    return NULL;  else    return new CBullet;}


Warning: Not all control paths return a value.

Remove the else and you'll be fine.




Blah! Be a man!!!!
CBullet *CreateBulletIfFire(bool isFiring){  return (!isFiring ? NULL : new CBullet);}


:)
I considered writing it like that myself but decided it might confuse lesser minds ;)
Quote:Original post by Helter Skelter
Blah! Be a man!!!!
CBullet *CreateBulletIfFire(bool isFiring){  return (!isFiring ? NULL : new CBullet);}


:)


why not
return isFiring ? new CBullet : NULL;


no need to use the negation operator! save the nanoseconds!

:-P
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
I'd imagine it would just use the reciprocal jump operation when compiled, so it's purely an aesthetic issue. Still checking NOT(...) seems harder to me so I agree with you!

This topic is closed to new replies.

Advertisement