Thread: Iterating over private vector

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    Iterating over private vector

    Hi,
    I'm trying to learn how to use C++ properly and I've run into a dilemma.

    If I have a class which has a private vector and I want to allow a client of this class to iterate over the objects in the vector how should this be done?

    Should I write a public function which return a reference to the vector? Should I return an iterator some how?

    Any snippets of code would be appreciated. Obviously no examples are required for the case where a reference is returned.

    I would like to know what the most correct way of doing this is and why.

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    Why does the client need to access private data? You might get ideas for better class interface if you post your code here. If there's a very good reason to let the user of the class access private data, you could declare a friend function; but it's contrary to the spirit of encapsulation.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    I'll try and simplify my example.

    Let's say there is a class Canvas which has a vector of shapes.

    Code:
    class Canvas
    {
    public: 
     addShape(const Shape &shape);
     deleteShape(...);
    
    private:
     vector<Shape> shapes;
    }
    Lets say the client may want to iterate through all the shapes in a canvas so they can do something with them - draw them for example.

    One option I guess is to add a getShape(int n); and size() function so the client can do something like this.

    Code:
    for (int i=0; i<canvas.size(); i++)
    {
     canvas.getShape(i).draw()
    }
    I don't know if this is the best way of doing something along those lines. I'm wondering if there are any better suggestions.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    It all depends on how much control you want to give the client code.

    Do you want to allow the client to add or remove shapes from the vector without using your addShape or deleteShape calls? If so, go ahead and expose the vector.

    Do you want to allow the client to modify existing shapes in the vector? If so, expose the vector's begin and end iterators.

    Do you only want your client to have readonly access into the vector? If so, expose the vector's const begin and end iterators.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    Do you want to allow the client to modify existing shapes in the vector? If so, expose the vector's begin and end iterators.
    How is something like this done?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Create begin and end functions in your class and let them return the begin and end iterators from the vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I'd also create a public typedef of the vector's iterator type to simplify the code on both your end and the client's end.
    Code:
    class Canvas
    {
    private:
     vector<Shape> shapes;
    public: 
     typedef vector<Shape>::iterator iterator;
    
     iterator begin() { return shapes.begin(); }
     iterator end() { return shapes.end(); }
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The client could just use auto, but sure.
    Exposing the const_iterators wouldn't be a bad idea, either, since otherwise the class wouldn't work with const functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    Thanks for the help. I will need to read up on how iterators work.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Keep in mind that by allowing access to the iterators you are essentially allowing access to the contents of the private container. This may or may not be what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  2. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  3. I need a code for Tic Tac Toe game ?
    By martyr in forum C++ Programming
    Replies: 11
    Last Post: 12-07-2003, 03:29 AM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 10:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM