From: Stephen Howe on
Hi

I frequently find that that I move std::vector member or array members outside of a class object simply because of the
difficulty of providing an interface to the member through the class interface. If it was a single object, it would not be a
problem. I wish to traverse the said member using lower_bound() and upper_bound() in tandem.

A friend function could be an answer here.

Do I need the iterator pattern as mentioned in Gang of 4?

Thanks

Stephen Howe

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran on
On Jun 18, 9:09 pm, Stephen Howe
<sjhoweATdialDOTpipexDOT...(a)giganews.com> wrote:
> Hi
>
> I frequently find that that I move std::vector member or array members outside of a class object simply because of the
> difficulty of providing an interface to the member through the class interface. If it was a single object, it would not be a
> problem. I wish to traverse the said member using lower_bound() and upper_bound() in tandem.

If you find yourself wanting such things often, you are either
programming to the implementation too much, either the class that
contains the vector has too much responsibilities.

Ask yourself this: in the vicinity of your need to do iteration, what
else do you need from your class that contains the container? If
nothing/little, perhaps a judicious application of interface
segregation principle is in order.

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On Jun 18, 8:09 pm, Stephen Howe
<sjhoweATdialDOTpipexDOT...(a)giganews.com> wrote:
> Hi
>
> I frequently find that that I move std::vector member or array members outside of a class object simply because of the
> difficulty of providing an interface to the member through the class interface. If it was a single object, it would not be a
> problem. I wish to traverse the said member using lower_bound() and upper_bound() in tandem.

Is your object logically a range? Then make it a range.
Is it an object that has a range associated with it? Then provide a
member function that returns that range.

There you go, done.


> A friend function could be an answer here.
>
> Do I need the iterator pattern as mentioned in Gang of 4?

No, you need the iterator concepts as defined in the C++ standard
library, or better yet, the range concepts as defined in C++0x or
boost.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Andre Kaufmann on
Stephen Howe wrote:
> Hi
>
> I frequently find that that I move std::vector member or array members outside of a class object simply because of the
> difficulty of providing an interface to the member through the class interface. If it was a single object, it would not be a
> problem. I wish to traverse the said member using lower_bound() and upper_bound() in tandem.

I wouldn't return either the vector member (as a reference) or any
iterators or ranges to the vector outside of the class. This is IMHO
inherently unsafe, because it builds an unsafe coupled hierarchy, which
will break if the vector itself is changed.

E.g. if you use the iterators, while any other function modifies the
vector. This will happen more frequently/accidentally if you move any
iterator outside the class.

The best solution (to prevent any hard to find memory corruptions) is
IMHO to use a functional programming style by using lambda functions.

E.g. you don't return any iterators but call later a class member
function, which has a lambda function argument, which traverses the
vector and executes operations with the element(s) needed directly
without returning iterators.

You need a C++0x compiler for that.

If you can't use the new standard you should:

a) Use and return only const vector references
b) Use a STL which supports debug iterators to catch any problem quickly
c) Use helper objects to wrap the access to iterators / ranges and check
if the original vector has been modified
d) Prefer ranges over iterators

e) Best option is to return copies - elements itself or a
sub-vector-copy.

But beware, this will be time intensive and may degrade the
performance of your code significantly.

But regarding decoupling/error prevents it's the best solution
(without being able to use functional programming style).


> A friend function could be an answer here.
> Do I need the iterator pattern as mentioned in Gang of 4?
>
> Thanks
> Stephen Howe

Hope it helps,
Andre

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]