"Patrick" <googleaddress@yahoo.co.uk> wrote in message
news:94740f05.0402231635.5b02644e@posting.google.c om...[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message[/color]
news:<c1dj9o$1hkqvk$1@ID-196037.news.uni-berlin.de>...[color=blue][color=green]
> > "Patrick" <googleaddress@yahoo.co.uk> wrote in message
> > news:94740f05.0402230919.4c2ee747@posting.google.c om...[color=darkred]
> > > In class *ClassA* below I have an STL Vector *vec* as a member
> > > variable of the class.
> > > Do I have to create a destructer, and somehow deallocate the memory
> > > from *vec*, or is this handled automatically?[/color]
> >
> > It's handled automatically. Have confidence in C++, if it really was the[/color][/color]
way[color=blue][color=green]
> > you are worried about then that would a total nightmare and I would give[/color][/color]
up[color=blue][color=green]
> > C++ and start programming Java.
> >[color=darkred]
> > >
> > > Also I want to allow a client of *ClassA* to be able to view the
> > > elements of the vector.
> > > Returning a reference to *vec* would be bad OO programming as i would
> > > be returning a reference to a private member of the class.
> > > I was thinking of using a method that would return an iterator over
> > > the Vector i.e.
> > >
> > > vector<int>::const_iterator ClassA::getIterator()
> > > { return vec.begin();
> > > }
> > >
> > > However i realised then that STL iterators dont have a reference to
> > > the last element in the container, so this isnt viable.[/color]
> >
> > Huh, what gave you that idea?
> >[color=darkred]
> > > Also this is
> > > pretty much the bad OO programming in the sense that while its a const
> > > iterator and the Vector itself cant be modified, the elements of the
> > > vector can be modified, i.e. once again allowing a client to modify
> > > private data of the class.[/color]
> >
> > The elements of the vector cannot be modifed because you are returning a
> > const_iterator.
> >[color=darkred]
> > >
> > > Is there a design pattern or some construct of allowing the client to
> > > view elements of the vector in a "read-only" fashion, while not
> > > violating OO principles?...other than copying the entire vector
> > >[/color]
> >
> > Yes its called a const_iterator. Somewhere, somehow, you obviously know[/color][/color]
this[color=blue][color=green]
> > or you wouldn't have chosen to use a const_iterator.
> >[color=darkred]
> > > any help appreciated
> > >
> > > pat
> > >[/color]
> >
> > john[/color]
>
>
> Thanks for the replies, im new to c++ hence my confusion.
>
> John you seemed to be hinting that Iterators in C++ do contain a
> reference to the last element in the container, is this so?[/color]
An iterator can 'point to' any element of a container, or
one past the last one.
[color=blue]
> I have
> been searching for info on this but all the examples i have seen use
> the container to explicitly get the reference to the last element.[/color]
Get this book:
www.josuttis.com/libbook
I found that it paid for itself in a single day.
[color=blue]
> As for the const_iterator, maybe i am wrong, but from what i read[/color]
May I ask what you're reading?
[color=blue]
> i
> understood that a cont_iterator only prevented one from carrying out
> operations on the container itself e.g. adding elements to the vector,
> removing elements from the vector.[/color]
No. A 'const_iterator' disallows modification of what it points to.
Similar to a pointer to const, e.g. 'const int *'
[color=blue]
>
> I understood that a const_iterator does not prevent a client from
> altering objects contained withing the container.[/color]
You understand incorrectly. :-)
[color=blue]
>
> So if the vector contained within my *ClassA* contains objects of type
> *ClassB*, when i return an iterator to a client of *ClassA* it will
> prevent the client from carrying out (destructive) operations on the
> vector[/color]
on the vector's *elements*. (That is if you use a const_iterator)
[color=blue]
>composed within *ClassA*, however it will not prevent the
> client from carrying out destructive operations on the objects of type
> *ClassB* that are contained within the vector. Is this not true?[/color]
You've got it exactly backwards. :-)
-Mike