using a custom class in a list

C

cppaddict

What do I have to do in order to create a list of one of my own classes?
That is, in order for:

list<myClass> myList;

to be a valid statement. I know I might have to define an iterator on it,
or implement operators like ++, but I don't know the details. Can anyone
let me know, or point to a good reference on the web?

Thanks,
cppaddict
 
J

Jonathan Mcdougall

What do I have to do in order to create a list of one of my own
classes? That is, in order for:

list<myClass> myList;

to be a valid statement.

Only the name 'myClass' defined :

# include <list>

class myClass
{
};

int main()
{
std::list<myClass> myList;
}

And that's it (try it).
I know I might have to define an iterator
on it, or implement operators like ++, but I don't know the details.

God, no!! That's why there is a library already written for you.

Iterators do not depend on the contained object, but on the container.
Operators concerning that iterator are defined by that iterator, no by you.
Can anyone let me know, or point to a good reference on the web?

Concerning the standard library, get "The C++ Standard Library" by Josuttis.


Jonathan
 
V

Victor Bazarov

cppaddict said:
What do I have to do in order to create a list of one of my own classes?
That is, in order for:

list<myClass> myList;

to be a valid statement. I know I might have to define an iterator on it,
or implement operators like ++, but I don't know the details. Can anyone
let me know, or point to a good reference on the web?

You need to

a) Include the <list> header
b) Declare std::list so that it could be named 'list' (no std::)
c) Make sure your 'myClass' is
1) Assignable
2) Copy-constructible

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top