473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Special iterator

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
3 1574
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
2
by: Bobo | last post by:
Hello, all. I have just wrote the following little class, and I'd like you to give me your opinion about it. I compiles with VC6 but I'm not sure if it is totally right. I'm trying to explain...
2
by: JohanS | last post by:
Hi I have created my own linked list where last always points to first so the list is like a circle that goes round and round. Now i wanna use the list from the STL instead but i still want...
15
by: zealotcat | last post by:
Hi, all: I want to implement a function (using std::string and std::vector) split a string contain special token into vector. eg: string = "alex delia john" ==> vector = "alex"; vector =...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
4
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.