Archived

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

Stefaniii

Data Structure in C++

Recommended Posts

Stefaniii    122
I''m more experienced in Java then C++. In Java there is one giant super class called "Object". This is really usefull because it means you can create a data structure like a linked list to take Objects and then apply it to anything you like by using casts. So for example if I have a class Monster I could do: Monster m1, m2; list.add(m1); // add the monster to the list m2 = (Monster)list.get(); list.get returns an object which I cast into a monster. Meanwhile somewhere is defined a LinkedList class: class LinkedList { public void add(Object a) {} public Object get() {} } So now my question is how do I do this in C++? There is no super object in C++. If there is no way to make a general linked list (or any other data structure) than I''d have to rewrite it for every single type of object I''ll ever use. Will casting pointers to an object to (void*) work for this purpose? If so, is it a good way to solve this problem?

Share this post


Link to post
Share on other sites
Ready4Dis    180
Look into the STL and list/vector''s... that has what you want. Also, I have written my own templated linked list class that does similar (but allows me a bit more flexibilty than what STL has).

Share this post


Link to post
Share on other sites
Ready4Dis    180
By the way, it''s as simple as creating a list like this:

STL::list somelist;

Creates a list of integers, then you can

somelist.push_back(10); This would put the # 10 into the list (of course you can create a list of ANY type you wish, like your own custom classes and stuff). Then you must use an iterator to go through the list, but if you find any tutorial on lists (and or vectors) you will find a lot of information.

Share this post


Link to post
Share on other sites