Data protection question

This is a discussion on Data protection question within the C++ Programming forums, part of the General Programming Boards category; If I have a PRIVATE Node (struct) as part of my List class. I also have a PUBLIC Iterator class ...

  1. #1
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127

    Data protection question

    If I have a PRIVATE Node (struct) as part of my List class. I also have a PUBLIC Iterator class as part of my List class.

    I have the following dilemma:

    Iterator looks like this:
    Code:
    class Iterator{
    private:
    Node* curr;
    public:
    Iterator(Node* c = 0) : curr(c) {}
    
    /* etc/operators */
    
    Node* get_curr() const ( return curr; }
    I wrote this function (get_curr()) so in my implementation I could do the following:
    Code:
    /* iterate the list to some specific position */
    Node* temp = i.get_curr();
    where "i" is an iterator object.


    Outside of my class, Node is private, but a user CAN use the get_curr() function (though they can never return a Node outside the class.)

    Is THIS BAD?

  2. #2
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    BTW, this is my value look-up method:
    Code:
    const T& operator* const { return curr->data; };

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    To me it sounds like you're doing it a little backward. The point of an iterator as far as I can see is simply to provide access to parts of the data structure, instread of resorting to raw pointers or some other hack.

    You were sort of on the right track with get_curr(). Since you're building a list out of nodes, doesn't it make sense to have the iterators dereference operator return some reference to a node, and let node manage it's own data and access priveleges?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think iterators were meant to give access to data but to hide the implementation details of the container. Iterating a vector, list and set all looks the same. The end user stores data in a list, not nodes.

    If you give out nodes, you are simply risking that the user will corrupt the list.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    I think iterators were meant to give access to data but to hide the implementation details of the container. Iterating a vector, list and set all looks the same. The end user stores data in a list, not nodes.

    If you give out nodes, you are simply risking that the user will corrupt the list.
    Not to mention that you can't change your node implementation later on, since the Node datatype is shared between your implementation and the application using that Node data structure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    well this is really the only opportunity for someone to even mention a node:
    Code:
    List<int>::Iterator iter2 = someList.at(iter1.get_curr());

    As you can see, the only thing they CAN DO is use it to create another iterator.


    THIS can NEVER happen:
    Code:
    Node* someNode = someIter.get_curr();

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    However, linked lists are not particularly good for random access, and the at method is very strange. One would expect it to take an integer-type and return a value-type (get the n-th thing in the container). I can't undestand how your code differs from copy construction:
    Code:
    List<int>::Iterator iter2 = iter1;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    the header file is kinda long, so maybe i could PM to someone if they're interested and they could critique it, telling me what's good and what's not so good, and what i could do differently that might make it better?

    anyone interested?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  2. Replies: 1
    Last Post: 06-07-2002, 11:22 AM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21