Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!

How to Best Implement Objects with "Optional" Functionality?

  • You cannot reply to this topic
No replies to this topic

#1 Sean_Seanston   Members   -  Reputation: 509


Posted Today, 02:39 PM

I have a few ideas of how this might be implemented, but because I'm not sure if some ways are obviously better than others and because it could involve pointers, I thought I'd ask...


Let's say we have a situation in a game where one object may possess a certain feature or may not. An example that's very close to what I'm actually trying to do would be something like:

- We have a Location class.

- A Location may contain(or reference, w/e) zero or exactly one Safehouse object.

- Depending on whether or not the Location has a Safehouse when the player is interacting with the Location, the game/UI/menu/whatever will display different options to the player.

i.e. Right now I'm working off the assumption that if a Menu should display certain things based on the existence of a Safehouse or not, then the Menu should be able to query somehow whether or not a valid Safehouse actually exists.


I can see some different ways of accomplishing this, but I'm not sure which are reasonable and which should be avoided:


1. What I would have done in the past, and the first idea to come to me, was to use a simple pointer. I'd point it to a Safehouse if it existed, otherwise it would be set to null, and so the game could check if it was null and know whether or not a valid Safehouse existed in that way.

My problem with this is that I'm afraid of it being bad practice, and of memory leaks where the risk is probably not necessary.


2. I could use smart pointers, but I don't currently have a compiler that supports C++11 due to being on Vista, so it would have to be auto_ptr. Does that sound like a potentially good idea in this situation?


3. Use a vector. Then if the vector is empty, there are no valid Safehouse objects and there's no fear of trying to call a member function of a null pointer or dealing with pointer allocation/deletion.

My problems with this are that it would seem to complicate matters such as dealing with encapsulation (accessing the elements without it being long-winded), and technically... I know it's not a big problem to just use a container and leave the possibility open of eventually having more than one, but as a learning experience I'm somewhat avoiding the issue.

What IF I definitely only wanted a single object? What would be good practice then?


4. Use a static Safehouse object in each Location. Use some sort of flag to denote whether or not it's valid. Seems ugly to me, and wasteful though that probably won't be a practical concern here anyway. Then again, maybe this is the only real solution if I demand a single object only and no pointers?


Looking at these, I'm leaning towards 3. Only thing is, something that always trips me up in situations like this: I'm not sure what's good practice for accessing a container outside the containing class.

e.g. Let's say we have a container of purchasable items, and another class needs to display information about these items. Is it best to just have the whole container returned by const reference?

Perhaps we're meant to avoid needing to access a container like this from outside... but then we can't decouple the item data from the user interface code, which to me sounds like it would be more important.