Page 1 of 2
iters
Posted: Tue Oct 02, 2007 11:05 am
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??? */
};
Re: iters
Posted: Tue Oct 02, 2007 11:49 am
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).
Posted: Tue Oct 02, 2007 12:42 pm
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.
Posted: Tue Oct 02, 2007 1:10 pm
by eboyd
also, how can I define prototypes for node & or iter in an implementation file?
Posted: Tue Oct 02, 2007 1:41 pm
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).
Posted: Tue Oct 02, 2007 1:58 pm
by eboyd
gotya, thanks bunches. i'll leave you guys alone to do more important things now!
Posted: Thu Oct 04, 2007 9:25 am
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]
Posted: Thu Oct 04, 2007 9:28 am
by eboyd
nevermind, this fixed it:
Code: Select all
template <typename T> class aClass<T>::node
{
public: // I FORGOT THE PUBLIC HERE!
/* ... */
};
Posted: Thu Oct 04, 2007 10:15 am
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 "...".
Posted: Thu Oct 04, 2007 12:51 pm
by eboyd
should the iterator class be in the public or private section of list?
Posted: Thu Oct 04, 2007 1:13 pm
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
}
}
Posted: Thu Oct 04, 2007 11:10 pm
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++;
}
}
Posted: Fri Oct 05, 2007 9:38 am
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?
does this mean i have to overload the "*" operator?
Posted: Fri Oct 05, 2007 10:40 am
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();
};
does this mean i have to overload the "*" operator?
Yep.
Try searching for ISO 14882, chapter 21 (iirc).
Posted: Fri Oct 05, 2007 11:42 am
by eboyd
will this work:
Code: Select all
T& operator*()
{
return(aNode->getVal());
}