<jayesah@gmail.comwrote in message
news:1164371649.562271.119830@l12g2000cwl.googlegr oups.com...
Quote:
Hi All,
>
List and its iterator work as following way :
>
list<intmylist;
list<int>::iterator itr;
itr = mylist.begin();
cout << (*itr);
>
But I want something like this:
>
list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);
>
>
So I wrote MyIterator following way :
>
template <class Tclass MyIterator : public list<T>::iterator
{
public:
MyIterator() { };
>
~MyIterator() { };
>
MyIterator(list<T>& mylist) {
>
/* What I should write here */
};
};
>
int main()
{
list<intmylist;
mylist.push_back(1);
mylist.push_back(2);
>
/* I want to support following construct */
>
MyIterator iter(mylist);
cout <<*mylist;
>
return 0;
}
>
Can anybody please guide me what I should write in one parameter
constructor ? Or do you have all together different solution ?
>
The reason I derived MyIterator from list<T>::iterator is that I want
to support all the overloaded operator that list<T>::iterator supports.
>
Thanks
Jayesh Shah
I think the problem you're going to run into is that mylist really doesn't
know what it is. That is, you really want:
MyIterator iter(mylist);
to expand to
MyIterator iter(list<int>);
or the like (although that above won't compile I"m sure).
I understand why you want to do this, and it would be nice, but I really
don't think it's possible The way I deal with it is by using typedef.
typedef list<intDataList;
DataList MyList;
DataList::iterator it = MyList.begin();
std::cout << (*itr);
I don't really think you can generically create an iterator off a generic
container. It would be nice if you could, but still, what if it's a map?
Then you have .first() and .second() which a list doesn't have, etc...