iters

Programming, for all ages and all languages.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

iters

Post by eboyd »

should i implement the STL iterator or should I create my own version?

regardless of the answer to the above, where do you put an iterator class?
in the public or private sections?

Code: Select all

class list
{
    struct node
    {
    };

    /*  DOES ITERATOR CLASS GO HERE???  */

    public:

    /*  OR DOES IT GO HERE???  */

};
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: iters

Post by Candy »

eboyd wrote:should i implement the STL iterator or should I create my own version?

regardless of the answer to the above, where do you put an iterator class?
in the public or private sections?

Code: Select all

class list
{
    struct node
    {
    };

    /*  DOES ITERATOR CLASS GO HERE???  */

    public:

    /*  OR DOES IT GO HERE???  */

};
Public, or you won't be able to reference iterator outside of list. Put node in the private section though, it needn't be referenced (other than by list and iterator, that is).
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

[quote=Candy]Put node in the private section though, it needn't be referenced (other than by list and iterator, that is).[/quote]

Isn't in the private section the way I have it? I thought classes were private by default and structs public by default.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

also, how can I define prototypes for node & or iter in an implementation file?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

eboyd wrote:also, how can I define prototypes for node & or iter in an implementation file?
It is in the private section, I tend not to rely on the order implicitly, I make it explicit. Also, as a matter of logic I put public members at the top - if you read it, you want the interface first and then the nitty gritty details of the implementation.

This way:

Code: Select all

// header
class a{
   class node;
   class iter;
   iter function();
};

//code
class a::iter {...};
class a::node {...};
Just a small note, you want iter in the header since you need it if you call function() (and you can't just include the code file).
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

gotya, thanks bunches. i'll leave you guys alone to do more important things now!
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

Code: Select all

class a
{
    public:
        class iter;

    private:
        class node;
};

class a::iter{...};

class a::node{...};
I can define the public iter no problems this way, but I'm having troubles with node. Does being *private* make a difference?[/code]
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

nevermind, this fixed it:

Code: Select all


template <typename T> class aClass<T>::node
{
    public:   // I FORGOT THE PUBLIC HERE!

    /*    ...     */

};
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

eboyd wrote:nevermind, this fixed it:

Code: Select all


template <typename T> class aClass<T>::node
{
    public:   // I FORGOT THE PUBLIC HERE!

    /*    ...     */

};
80-100% of the problems you can't find is in the code you would shorten to "...".
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

should the iterator class be in the public or private section of list?
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

I'm also running into problems when trying to implement the Iterators in List member functions.

Code: Select all


template <typename T> void List<T>::display()
{
    /*   How do I declare the Iterator?  */

    /*  I want to do something like this:
    */
    while( myIter != end )
    {
        /*  How can I display the Node data here using an Iterator?  */
     
        myIter++;  //  Advance to the next node in the list
    }
}
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

eboyd wrote:should the iterator class be in the public or private section of list?
Do you want list to be the only user of iterator?
eboyd wrote:I'm also running into problems when trying to implement the Iterators in List member functions.

Code: Select all

template <typename T> void List<T>::display()
{
    iterator myIter = begin();

    while( myIter != end() )
    {
        display(*myIter);
     
        myIter++;
    }
}
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

Ok, I think I'm getting this...

Code: Select all

...

iterator myIter = begin();

    while( myIter != end() )
    {
        ...
    }

    ...

begin() & end() are member functions of iterator, right?


Code: Select all

display(*myIter);
does this mean i have to overload the "*" operator?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

eboyd wrote:Ok, I think I'm getting this...

Code: Select all

...

iterator myIter = begin();

    while( myIter != end() )
    {
        ...
    }

    ...

begin() & end() are member functions of iterator, right?
Member functions of list, returning an iterator:

Code: Select all

class list {
    class iterator;
    iterator begin();
    iterator end();
};

Code: Select all

display(*myIter);
does this mean i have to overload the "*" operator?
Yep.

Try searching for ISO 14882, chapter 21 (iirc).
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

will this work:

Code: Select all

T& operator*()
      {
         return(aNode->getVal());
      }
Post Reply