using a custom class in a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cppaddict

    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


  • Victor Bazarov

    #2
    Re: using a custom class in a list

    "cppaddict" <cppaddict@yaho o.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


    Comment

    • Jonathan Mcdougall

      #3
      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<myCla ss> 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



      Comment

      Working...