giving a class a standard iterator interface

C

ChasW

I have a class that manages a resource and provides a pointer to that
resource. For the sake of simplicity in asking this question:

class myClass
{
public:
myClass() { m_ptr = 0; }
~myClass();

unsigned char* getPtr { return m_ptr; }

unsigned char* operator+ ( long int n ) { return m_ptr + n; }

private:
m_ptr;
};

Notice that the class has an operator+ overload that returns a raw
pointer. This could be problematic for code that attempts to deduce
the type of an object of myClass type when seeing it used with the +
operator. Yet I want the functionality to be the same regardless, so I
need to give this class Random Access Iterator functionality.

I am not new to using iterators with STL containers and STL
algorithms, but what I want to do is either one of the following:

1) provide that set of functionality that makes my class an iterator
to its own resource, or
2) have myClass only manage the resource and have a separate class to
serve as an iterator to my class.

I am not sure exactly how to do either. What I need here is a
kickstart.

Do I inherit an iterator?
Do I add one as a member?
What would the prototype for overloading operator+ look like?
Would that overloaded operator+ return an iterator of unsigned char*
type or myClass type?

Thanks in advance for any helpful responses,
Charles Wilkins
 
C

ChasW

I have a class that manages a resource and provides a pointer to that
resource. For the sake of simplicity in asking this question:

class myClass
{
public:
myClass() { m_ptr = 0; }
~myClass();

unsigned char* getPtr { return m_ptr; }

unsigned char* operator+ ( long int n ) { return m_ptr + n; }

private:
m_ptr;
};

Sorry for the typo. It would help if i gave the private member a type
:)

private:
unsigned char* m_ptr;

Charles
 
I

Ian Collins

ChasW said:
I have a class that manages a resource and provides a pointer to that
resource. For the sake of simplicity in asking this question:

class myClass
{
public:
myClass() { m_ptr = 0; }
~myClass();

unsigned char* getPtr { return m_ptr; }

unsigned char* operator+ ( long int n ) { return m_ptr + n; }

private:
m_ptr;
};

Notice that the class has an operator+ overload that returns a raw
pointer. This could be problematic for code that attempts to deduce
the type of an object of myClass type when seeing it used with the +
operator. Yet I want the functionality to be the same regardless, so I
need to give this class Random Access Iterator functionality.

I am not new to using iterators with STL containers and STL
algorithms, but what I want to do is either one of the following:

1) provide that set of functionality that makes my class an iterator
to its own resource, or

Your class looks more like like an iterator than a container. It could
be the start of an iterator to something that holds an array of char.
2) have myClass only manage the resource and have a separate class to
serve as an iterator to my class.
The class doesn't hold anything that can be iterated over.
I am not sure exactly how to do either. What I need here is a
kickstart.

Do I inherit an iterator?

Start simple, adapt your class (add an operator * and the other
mathematical operators) to be an iterator for an array and see where you
end up.
Would that overloaded operator+ return an iterator of unsigned char*
type or myClass type?
If your class is an iterator, it should return an instance of its self
so you can use consistently when the data member is something more
complex than a simple pointer.
 
J

Jeff F

I have a class that manages a resource and provides a pointer to that
resource. For the sake of simplicity in asking this question:

class myClass
{
public:
myClass() { m_ptr = 0; }
~myClass();

unsigned char* getPtr { return m_ptr; }

unsigned char* operator+ ( long int n ) { return m_ptr + n; }

private:
m_ptr;
};

I assume the type of m_ptr is unsigned char*? I don't see much management
going in the code posted here?
Notice that the class has an operator+ overload that returns a raw
pointer. This could be problematic for code that attempts to deduce
the type of an object of myClass type when seeing it used with the +

Not sure what you're getting at here?
operator. Yet I want the functionality to be the same regardless, so I
need to give this class Random Access Iterator functionality.

A pointer already has 'Random Access Iterator functionality'. It can be used
with std algorithms.
I am not new to using iterators with STL containers and STL
algorithms, but what I want to do is either one of the following:

1) provide that set of functionality that makes my class an iterator
to its own resource, or
2) have myClass only manage the resource and have a separate class to
serve as an iterator to my class.

I am not sure exactly how to do either. What I need here is a
kickstart.

I think you need to clarify what you're really trying to do. What is the
reource? What do you really want to iterate?

Then you should see the boost iterator library at
http://www.boost.org/libs/iterator/doc/index.html. Probably you should start
with iterator adaptors described at
http://www.boost.org/libs/iterator/doc/iterator_adaptor.html.

Jeff Flinn
 
C

ChasW

in message news:[email protected]...

I assume the type of m_ptr is unsigned char*? I don't see much management
going in the code posted here?
Yes, m_ptr is unsigned char* as I posted immediately after my initial
post. I guess you did not see it.

The management code was intentionally left out so as not to overshadow
the question at hand, which is how to give a class an iterator
interface. The code posted was merely to illustrate the current state
of how the value of that private member was being accessed.

I should be able to trust you read the second sentence in this post
which points out why.
Not sure what you're getting at here?
Simply put, the overload of operater+ returning something other than
*this does not properly emulate how iterators work.

What I am (was) looking for was a set of guidelines for overloading
the operators similarly to how random access iterator operators are
overloaded, so that the class can meet those guidelines and therefore
be used as a random access iterator.

The point of my confusion was whether or not to give the class its own
iterator type or allow the class itself to be the iterator and how,
exactly is that done. Both solutions are valid.

I found this:
http://www2.sscc.ru/Links/Litera/docs/online/stdlibcr/Ran_7047.htm
which so far has been somewhat helpful, but I'm still having to study
various implementations to see the overview. Overloading unary
operator* for example:

unsigned char & operator*() const;
A pointer already has 'Random Access Iterator functionality'. It can be used
with std algorithms.
Completely not the point. The topic is giving a class an iterator
interface, not how do pointers work and an iterator certainly is not a
pointer.
I think you need to clarify what you're really trying to do. What is the
reource?
Actually, I don't. Since I now know the answer, I can say reasonably
that the details of the question were more than sufficient. The other
gentleman that responded did not have any trouble providing a helpful
response which actually led me to the solution.
What do you really want to iterate?
The managed resource. That should be sufficient. Questions like this
are often times necessary, but they can also often times be a sink
hole in a newsgroup that does not take kindly to off topic questions.
I expressed my question to be a C++ question and only a C++ question
with good intent.
Then you should see the boost iterator library at
http://www.boost.org/libs/iterator/doc/index.html. Probably you should start
with iterator adaptors described at
http://www.boost.org/libs/iterator/doc/iterator_adaptor.html.

Jeff Flinn

Thanks for the "drive-by" advertisement of boost, but boost is not
required to give a class an iterator interface and it certainly is not
the answer to the question.

Don't get me wrong, boost has some really cool stuff, but it just does
not apply to simple nature of this question.

Best regards,
Charles
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top