In my application, I have a function foo() that has some local vector objects. I need to call a function bar() from this function numerous times. The function bar() needs to access those vector objects.
I know I can pass by reference or I can declare those variables as global.
Is there any other way in which I can implement this so that my program does not get polluted with global variables?
This will only make sense of course if the vectors have something in common or are part of an entity. Again, it's hard to judge without knowing more specifics about your program. In general I would advise against global variables or befriending things unless there is a very specific reason. Although they don't necessarily mean bad design, they are often a symptom of it if used improperly.
For more information about classes in C++ you can have a look at following articles:
http://www.cplusplus.com/doc/tutorial/classes/
However, putting the two functions together in a class, just to avoid having to pass the vectors as parameters, only makes sense if the class actually represents some sensible entity with a meaningful state.
What's wrong with just passing the vectors in as function parameters?
@MikeyBoy
I was worried about the time complexity. If I pass by reference then would the parameters be passed without any overhead? I need to call the function bar() multiple times from foo() and time is going to be a major constraint in my program.
No I am just in the design phase right now. So I'll first try to pass those vectors by reference. If there is a significant performance issue then I'll implement the class way as others suggested.
Though I'm also curious about the way keskiverto suggested. How can lambda functions be used?
A lambda function is not really meant to circumvent this kind of thing.
The idea behind passing by reference is that you are really only passing just that, a reference into the function. This means the whole vector does not need to be copied, but the function can alter it in its original place in memory.
For completeness I should state that although this seems to be an accepted practice in C++ programming (you see it all the time) there are people who disagree: http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/
If some of the experts here could perhaps shed their light on both approaches it could be helpful to more than just the OP.
I only skimmed that article, but it looks as though it only applied to specific circumstances where rvalues are involved. It may or may not be useful for the OP's situation, since s/he hasn't shown us the specifics of their code.
I believe the RV talked about in the article is return values (hence return value optimization), rather than rvalues. Please correct me if that's what you meant, it's easy to get confused in these things.
From what I gather from that article the author states that compilers use return value optimization is such a way that passing or returning by value is actually not expensive at all.
Again, I hope some of the experts here can chime in and shed their light on it.