giving a class a standard iterator interface

Discussion in 'C++' started by ChasW, Jan 24, 2008.

  1. ChasW

    ChasW Guest

    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
     
    ChasW, Jan 24, 2008
    #1
    1. Advertising

  2. ChasW

    ChasW Guest

    On Thu, 24 Jan 2008 00:28:45 -0500, ChasW <> wrote:

    >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
     
    ChasW, Jan 24, 2008
    #2
    1. Advertising

  3. ChasW

    Ian Collins Guest

    ChasW wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Jan 24, 2008
    #3
  4. ChasW

    Jeff F Guest

    "ChasW" wrote in message news:...
    >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
     
    Jeff F, Jan 25, 2008
    #4
  5. ChasW

    ChasW Guest

    On Thu, 24 Jan 2008 20:26:24 -0500, "Jeff F" <>
    wrote:

    >
    >"ChasW" wrote in message news:...
    >>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?
    >

    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.

    >> 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?

    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;

    >> 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.

    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 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?

    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
     
    ChasW, Jan 25, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cyberco
    Replies:
    8
    Views:
    507
    cyberco
    Feb 25, 2006
  2. Greenhorn
    Replies:
    0
    Views:
    305
    Greenhorn
    Mar 3, 2005
  3. Greenhorn
    Replies:
    0
    Views:
    300
    Greenhorn
    Mar 5, 2005
  4. sixteenmillion

    The giving that keeps on giving

    sixteenmillion, Nov 19, 2007, in forum: C Programming
    Replies:
    0
    Views:
    449
    sixteenmillion
    Nov 19, 2007
  5. Dave
    Replies:
    8
    Views:
    515
    Bo Persson
    Dec 14, 2009
Loading...

Share This Page