views:

83

answers:

4

How do I do that? Like you know in Java, you can use an ArrayList and it will take any object as long as you cast it down to whatever it is when you're retrieving the object.

Even better, you can specify what class of objects that ArrayList would store by doing...

new ArrayList()< whateverObject >

I've implemented a linked list data structure in C++ and I'd like to know how I can allow it to do this...

At the moment, I'm just using...

typedef whateverObject ItemType

at the start of my header file for my linked list and then manipulating "ItemType" throughout the implementation of the linked list. So every time I want to change the type, e.g. instead of using the list for storing strings, I want to store an int, I'll have to change the typedef in my linked list's header but I want to be able to simply use it for any object so...

How?!

Thanks.

+4  A: 

If I have understood well what you ask, templates is what you want.

Take a look here:

http://www.cplusplus.com/doc/tutorial/templates/

DarkFire21
cplusplus.com is really a terrible learning resource.. I find the way they explain thing very confusing, and sometimes inaccurate too. (But it's obviously just my opinion, and it's a valid answer so no downvote)
Andreas Bonini
It's good for a reference. That's all.
the_drow
cplusplus.com is actually the first place I've ever been to learn programming... Learnt a little C++ there and got a bit of the fundamentals down before now learning Java in school.I think they're OK but really... terrible? It was one of the best I could find when I wanted to start learning about programming because most of the other guides or tutorials were either really poorly presented or wasn't free.... or maybe I wasn't searching hard enough.
Dois
I am not fan of cplusplus.com either, but I think its a good resource for a beginner to take a first "taste".Anyway...
DarkFire21
+5  A: 

Use templates. It's a lot to explain so I'll just give you a link where it's explained much better than I'll ever be able to do here: C++ FAQ - Templates.

While you're at it, if you have the time, I suggest you read the whole FAQ, it's really a great resource!

Andreas Bonini
+5  A: 

Templates are the answer to your question.

Define your linked list as follows :

template<typename ItemType>
class ArrayList
{
  // What's inside your class definition does not need to be changed
  // Include your method definitions here and you'll be fine
};

The type to use is then ArrayList<WhateverObject>.

Benoît
Thanks everyone for the swift help.
Dois
+2  A: 

In java you can do so, because all classes are inherited from one base class Object. In C++ you do not have it. The reason is that Object base class impose overhead for all objects, while C++ do not like any unnecessary overhead. If you want to store any object - you can store "void *" data type. The question remained - what you will be able to do with objects, without the knowledge of the type? If you do know - you can cast to the needed type and use it. The practice described above is not safe, and templates are better in most cases.

David Gruzman