Special iterator

Discussion in 'C++' started by KK, Dec 16, 2005.

  1. KK

    KK Guest

    Hello comp.lang.c++,
    Is it possible to construct an iterator for a class that can traverse
    through all the elements associated with that class?
    Class AnotherDataType; //defined somewhere else
    Class Crazy
    {
    int _num;
    std::string _name;
    unsinged int _index;
    AnotherDataType _tmp;

    Class Iterator
    {
    //define other necessary funcs elements
    Iterator& operator++ () {
    // IF the current element pointer is "_num" THEN return
    pointer to "_name"
    // IF the current element pointer is "_name" THEN return
    pointer to "_index"
    // IF the current element pointer is "_index" THEN return
    pointer to "__tmp"
    }
    }
    }
    Any ideas on how to go about doing this?
    Thanks.
    -KK
     
    KK, Dec 16, 2005
    #1
    1. Advertising

  2. KK

    mlimber Guest

    KK wrote:
    > Hello comp.lang.c++,
    > Is it possible to construct an iterator for a class that can traverse
    > through all the elements associated with that class?
    > Class AnotherDataType; //defined somewhere else
    > Class Crazy
    > {
    > int _num;
    > std::string _name;
    > unsinged int _index;
    > AnotherDataType _tmp;
    >
    > Class Iterator
    > {
    > //define other necessary funcs elements
    > Iterator& operator++ () {
    > // IF the current element pointer is "_num" THEN return
    > pointer to "_name"
    > // IF the current element pointer is "_name" THEN return
    > pointer to "_index"
    > // IF the current element pointer is "_index" THEN return
    > pointer to "__tmp"
    > }
    > }
    > }
    > Any ideas on how to go about doing this?
    > Thanks.
    > -KK


    You could use boost::any (http://boost.org/doc/html/any.html) or
    boost::variant (http://boost.org/doc/html/variant.html) to accomplish
    this.

    Cheers! --M
     
    mlimber, Dec 16, 2005
    #2
    1. Advertising

  3. KK

    Guest

    KK wrote:
    > Hello comp.lang.c++,
    > Is it possible to construct an iterator for a class that can traverse
    > through all the elements associated with that class?
    > Class AnotherDataType; //defined somewhere else
    > Class Crazy
    > {
    > int _num;
    > std::string _name;
    > unsinged int _index;
    > AnotherDataType _tmp;
    >
    > Class Iterator
    > {
    > // IF the current element pointer is "_num" THEN return
    > pointer to "_name"
    > // IF the current element pointer is "_name" THEN return
    > pointer to "_index"
    > // IF the current element pointer is "_index" THEN return
    > pointer to "__tmp"


    No. There's no return type for operator*

    HTH,
    Michiel Salters
     
    , Dec 16, 2005
    #3
  4. KK

    mlimber Guest

    wrote:
    > KK wrote:
    > > Hello comp.lang.c++,
    > > Is it possible to construct an iterator for a class that can traverse
    > > through all the elements associated with that class?
    > > Class AnotherDataType; //defined somewhere else
    > > Class Crazy
    > > {
    > > int _num;
    > > std::string _name;
    > > unsinged int _index;
    > > AnotherDataType _tmp;
    > >
    > > Class Iterator
    > > {
    > > // IF the current element pointer is "_num" THEN return
    > > pointer to "_name"
    > > // IF the current element pointer is "_name" THEN return
    > > pointer to "_index"
    > > // IF the current element pointer is "_index" THEN return
    > > pointer to "__tmp"

    >
    > No. There's no return type for operator*


    Sure there is. That's how smart pointers work (cf. boost::scoped_ptr).

    Cheers! --M
     
    mlimber, Dec 16, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,429
  2. greg
    Replies:
    6
    Views:
    459
    Dietmar Kuehl
    Jul 17, 2003
  3. Bobo

    Special map iterator

    Bobo, Feb 9, 2004, in forum: C++
    Replies:
    2
    Views:
    410
  4. Mark Hobley

    Iterator special variable $_

    Mark Hobley, Jan 7, 2007, in forum: Perl Misc
    Replies:
    10
    Views:
    172
    Verizon
    Jan 10, 2007
  5. Mathematisch

    please help with creating a special iterator

    Mathematisch, Oct 18, 2010, in forum: Perl Misc
    Replies:
    4
    Views:
    108
    Xho Jingleheimerschmidt
    Oct 19, 2010
Loading...

Share This Page