Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

Null object in C++

This topic is 4728 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

I have a 5 by 5 square field in which each square can have an occupant. This is a small battle field and so there may only be 3 characters on the field at once and I was wondering what is the best way to represent a square's occupant if the square is empty? I tried using NULL but that seems to not work the way it does in java.

Share this post


Link to post
Share on other sites
Advertisement
I'm sure there are differences but it is not that different, in my opinion. What did you try exactly. The following would work okay:

class CSquare
{
protected:

COccupant* m_pOccupant;

public:

CSquare( void )
{
m_pOccupant = NULL;
}

COccupant* const GetOccupant( void )
{
return m_pOccupant;
}
};

CSquare* pSomeSquare = ...;
if ( pSomeSquare->GetOccupant() == NULL )
// No occupant in this square.



Maybe if you detail your question with an example or actual code, the precise difference or misuse/misinterpretation may emerge.

Greetz,

Illco

Share this post


Link to post
Share on other sites
ok I found my problem I had COccupant m_pOccupant instead of COccupant* m_pOccupant.

Is there another option then making all my objects pointers?

Share this post


Link to post
Share on other sites
Ok. The problem is that an instance of an object always has valid contents. Although they may not be what you want, there is no generic way of determining whether the object is occupied or not -- it always is. In Java all objects behave as if they are pointers to objects, although you never get to see a pointer.

Share this post


Link to post
Share on other sites
You could make a 3*3 matrix of Objects, call them for example Field, now you're
deriving von the Field class every different type of a field, player1, player2, player3, empty,... and let the compiler worry about the right thing, by using virtual functions.

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!