Connecting Tech Pros Worldwide Forums | Help | Site Map

using a custom class in a list

cppaddict
Guest
 
Posts: n/a
#1: Jul 19 '05
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



Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 19 '05

re: using a custom class in a list


"cppaddict" <cppaddict@yahoo.com> wrote...[color=blue]
> 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?[/color]

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


Jonathan Mcdougall
Guest
 
Posts: n/a
#3: Jul 19 '05

re: using a custom class in a list


> What do I have to do in order to create a list of one of my own[color=blue]
> classes? That is, in order for:
>
> list<myClass> myList;
>
> to be a valid statement.[/color]

Only the name 'myClass' defined :

# include <list>

class myClass
{
};

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

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

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.
[color=blue]
> Can anyone let me know, or point to a good reference on the web?[/color]

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


Jonathan



Closed Thread