Special iterator

K

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
 
M

mlimber

KK said:
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
 
M

Michiel.Salters

KK said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top