Archived

This topic is now archived and is closed to further replies.

DBX

Java collections in C++

Recommended Posts

I intend to write some collection classes in C++ similar to the ones in Java (i.e. a Vector, Linked List etc.). The Java ones store objects of type Object, since every class in Java is a subclass of Object. This works very nicely in my opinion. C++ doesn''t do this automatically, so I appear to have two options: Use void pointers, or create a base Object class which all classes I want to store subclass. It is unlikely that there will become a point at which I don''t know the type of the objects stored in the collection, so casting pointers is easy. Either of these seemed reasonable until I read (on a completely unrelated topic) this thread - http://www.gamedev.net/community/forums/topic.asp?topic_id=82118 where the general consensus was using a base object class, or void pointers was a really bad idea in all cases. Their arguments seemed fairly specious to me, but what do I know. So, what would be a "sensible" approach to designing some (general purpose, reusable) collection classes? Personally, I consider the principle used in the Java ones very good, but I don''t want to be bitten on the arse at some point in the future by starting out with a flawed design. (About templates, which I''m sure someone will suggest: I hold these in some contempt, at least as I believe them to work. Consider the STL vector class. If my application had to store, say, 10 different types (not necessarily in the same vector) in vectors, then I would specify 10 different types of vector class, and 10 lots of vector code would be produced. I could be very wrong here, and I apologise for my incompetence in advance)

Share this post


Link to post
Share on other sites
I don''t really see the point of this. If you want to program using Java idioms, use Java. I''ve never seen anything good come out of trying to impress a foreign idiom on a language that doesn''t really support it.
quote:
Original post by DBX
(About templates, which I''m sure someone will suggest:
I hold these in some contempt, at least as I believe them to work. Consider the STL vector class. If my application had to store, say, 10 different types (not necessarily in the same vector) in vectors, then I would specify 10 different types of vector class, and 10 lots of vector code would be produced.
I could be very wrong here, and I apologise for my incompetence in advance)


Nah, you are pretty much correct there.



AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.

Share this post


Link to post
Share on other sites
I''m afraid I''m going to suggest that you use the STL containers. While you are right about the template expansions, it''s not actually that bad. If you want to do it the ''Java way'' and have all your classes derived from a common base class, then you can simply use a vector, list, or whatever, of pointers to that base class, and you''ll only have one set of template expansions.

Share this post


Link to post
Share on other sites
Thanks for the help.

It''s not that I want to do it ''the Java way'' but that I like having the Collections class when I use Java, and the STL doesn''t cut it for me (not that I know much about it anyway). From what I can tell, the STL has no abstract classes (like Collection, List, Map etc..) to base the other classes off, and does not have many types - I would like to add my own.
Still, there''s nothing stopping me wrapping the STL classes.

I think that, shock horror, I might actually use templates. The reason for this is that I can simply make the decision about what type to use later, and maybe have the header files automatically create types using Object, but create other types if certain defined macro''s are present (i.e. create a PVector using void* if COLLECTION_USE_PVECTOR is defined). Seems more flexible.

Share this post


Link to post
Share on other sites