Jump to content
  • Advertisement
Sign in to follow this  
Jarwulf

class containers

This topic is 3173 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 objects from two or possibly 3 different classes I want to place in some sort of container so I can perform an action on all of them in a loop. I've been reluctant to have them all inherit off the same base class because they have some fundamental differences and I plan to have other classes inherit off them as well. I thought about an array of pointers but I figure that could only point to one type of class. Is there any better solution?

Share this post


Link to post
Share on other sites
Advertisement
What language are you using? If it's C++, I would just have them all inherit from the same base class. If they are related enough to want to store them in the same container, they are probably related enough to have them inherit from the same base class. The base class doesn't have to have any specifics, it could just be one method that you want each of them to implement so that you can modify them how you want based on what type of object it is.

If you are using Java, just have them implement an interface with the method you want to call on them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jarwulf
I thought about an array of pointers but I figure that could only point to one type of class. Is there any better solution?
3 arrays of pointers ;)

Seriously, if you don't want to use polymorphism/inheritance, then make one array for each type. This avoids the overheads involved in trying to figure out the actual type of the object in a single array.

You can also use a single array where each element in the array is a pair of the actual pointer and some kind of type identifier...

Share this post


Link to post
Share on other sites
You really have two (simple) options:
1) Don't put them into an array.
2) Make them all derive from some base class.

(2) is only a good option if having them all derive from a common base makes sense from a design standpoint; in order to know that we'd have to know more about these objects and their intended use in your code. Inheritance is not something to be used "just because" or as a way to force a square peg through a round hole, however.

(1) is likely the better option and would be my current suggestion without knowing anything more about your system. Types not sharing a common base are not taxonomically related and should thus be treated differently; there is nothing wrong with that. Even if you don't need to treat them any differently now, there is a higher than fair chance you will later.

Share this post


Link to post
Share on other sites
There is also Boost.Variant for a fixed number of different types, and Boost.Any which basically wraps some polymorphism around your types without forcing you to inherit your classes from the same base.

However, you can use two or three different arrays, and then perhaps use templates to implement the things that you want to do with the different types.

With Boost.Variant, you could put them in the same array and then write a templated visitor for all the functionality.

Boost.Any is probably a dead end here.

Share this post


Link to post
Share on other sites
You could use an interface here (which in C++ comes down to inheriting from an abstract base class), but it sounds like that's overkill if all it does is eliminating 2 loops. Without more information, I'd say that using 3 arrays is best for now.

What kind of objects are we talking about and what action do they have in common? What classes do you plan to inherit from them?

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!