Jump to content
  • Advertisement
Sign in to follow this  
YellowMaple

C++ question about returning null

This topic is 4707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Yes that's what you want.
CBullet *CreateBulletIfFire(bool isFiring)
{
if(!isFiring)
return NULL;
else
return new CBullet;
}

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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 position
LastActiveBullet = HandlePlayerFire( LastActiveBullet);

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Yeah, technically you should have "NULL" and not "0", althugh I think "0" is nicer to look at.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!