Programming, for all ages and all languages.
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Tue Oct 02, 2007 11:05 am
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??? */
};
Candy
Member
Posts: 3882 Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven
Post
by Candy » Tue Oct 02, 2007 11:49 am
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).
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Tue Oct 02, 2007 12:42 pm
[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.
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Tue Oct 02, 2007 1:10 pm
also, how can I define prototypes for node & or iter in an implementation file?
Candy
Member
Posts: 3882 Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven
Post
by Candy » Tue Oct 02, 2007 1:41 pm
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).
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Tue Oct 02, 2007 1:58 pm
gotya, thanks bunches. i'll leave you guys alone to do more important things now!
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Thu Oct 04, 2007 9:25 am
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]
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Thu Oct 04, 2007 9:28 am
nevermind, this fixed it:
Code: Select all
template <typename T> class aClass<T>::node
{
public: // I FORGOT THE PUBLIC HERE!
/* ... */
};
Candy
Member
Posts: 3882 Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven
Post
by Candy » Thu Oct 04, 2007 10:15 am
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 "...".
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Thu Oct 04, 2007 12:51 pm
should the iterator class be in the public or private section of list?
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Thu Oct 04, 2007 1:13 pm
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
}
}
Candy
Member
Posts: 3882 Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven
Post
by Candy » Thu Oct 04, 2007 11:10 pm
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++;
}
}
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Fri Oct 05, 2007 9:38 am
Ok, I think I'm getting this...
Code: Select all
...
iterator myIter = begin();
while( myIter != end() )
{
...
}
...
begin() & end() are member functions of iterator, right?
does this mean i have to overload the "*" operator?
Candy
Member
Posts: 3882 Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven
Post
by Candy » Fri Oct 05, 2007 10:40 am
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();
};
does this mean i have to overload the "*" operator?
Yep.
Try searching for ISO 14882, chapter 21 (iirc).
eboyd
Member
Posts: 97 Joined: Thu Jul 26, 2007 9:18 am
Location: United States
Post
by eboyd » Fri Oct 05, 2007 11:42 am
will this work:
Code: Select all
T& operator*()
{
return(aNode->getVal());
}