C++ question about returning null

Started by
17 comments, last by d000hg 18 years, 9 months ago
Hi, I'm working on making a Shooter (SHMUP) game in OpenGL. Right now, I have a class called player that represents the player spaceship. I also have a class called Actor and a class called bullet which inherits the Actor class. In the player class, I have a function to process input from the keyboard, and would like to return a bullet object if the 'fire' key is pressed. However, if it is not pressed, I would like to return null. However, I get errors saying that cannot convert from bullet to int or something along those lines. I was able to sort of get it working using pointers, but it seems as though even if it's returning null, it's still going through the constructor for the bullet object and this slowed the game down a lot. Can anyone tell me how to simply return null in C++ without having the constructor method carried out, or can anyone suggest a better design to implementing a player ship and the bullets fired from the ship?
Advertisement
You'll have to return a pointer to the bullet object.

if (bullet_required)
return createBulletPtr();
else
return NULL;

This seems obvious, am I missing something?
Yes that's what you want.
CBullet *CreateBulletIfFire(bool isFiring){  if(!isFiring)    return NULL;  else    return new CBullet;}
Note also that if you are going to have lots of bullets created and destroyed all the time then this method is not the best (unless you overload new) ...

However tackle this issue when and if you come across it, don't optimise prematurely!
Why not have the fire method on the spaceship class, create the bullet itself then call the method / function in your object-management to add it there and then, rather than returning a pointer?

Personally what I do is have a manager "addObject" function which takes a pointer to the base class (in your case, Actor), and adds it to the list (actually, it adds it to another list, which then gets added to the main list at the end of the frame).

Mark
NULL in c/c++ is defined:
#define NULL 0
so "return NULL" is the same as "return 0", which is in.

try something like:
[source lang=c++]bullet* checkToCreateBullet(int create){  if(create)    return new bullet(...);  else    return (bullet)NULL;}
-----------------------------------------Everyboddy need someboddy!
May I suggest going from value return to using an output variable or better yet iterator.
Im guessing you have:
Buller HandlePlayerFire(void){//loads of cool logic}


as have been pointed out returning a pointer can solve this but you would still need to store the pointer somewhere remember to clean it up etc. You're probably already storing your bullets in an array/vector/list, my suggestion is to make use of the iterator concept to simply insert new bullets in the container if the player is fireing the code would then look like
template <typename OutItr>OutItr HandlePlayerFire(OutItr dst){//logic add bullet via:*dst++ = Bullet(...);return dst;//this can be plenty convinent}


calling code would then be something like
HandlePlayerFire( std::back_inserter( BulletContainer));

or if you're using a plain array:
//assuming LastActiveBullet is the insertion positionLastActiveBullet = HandlePlayerFire( LastActiveBullet);

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Quote:Original post by someboddy
NULL in c/c++ is defined:
#define NULL 0
so "return NULL" is the same as "return 0", which is in.


That's only true for C++. It's not that simple in C:
#undef NULL#if defined(__cplusplus)#define NULL 0#else#define NULL ((void *)0)#endif
Yeah, technically you should have "NULL" and not "0", althugh I think "0" is nicer to look at.
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.

This topic is closed to new replies.

Advertisement