By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

Special iterator

P: n/a
KK
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

Dec 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Dec 16 '05 #2

P: n/a

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 '05 #3

P: n/a

Mi*************@tomtom.com 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

Dec 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.