Connecting Tech Pros Worldwide Help | Site Map

using a custom class in a list

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:11 PM
cppaddict
Guest
 
Posts: n/a
Default using a custom class in a list

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



  #2  
Old July 19th, 2005, 07:12 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


  #3  
Old July 19th, 2005, 07:12 PM
Jonathan Mcdougall
Guest
 
Posts: n/a
Default 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



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.