Can a function be a "friend" of another function?

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?
Hi there,

It's hard to tell without knowing the specifics of your program, but you may want to consider OOP (Object Oriented Programming).

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct example_class
{
    std::vector v1, v2;
};


void bar(example_class& ec)
{
    //use ec.v1 and ec.v2
}


void foo ()
{
    example_class instance_foo;
    bar(instance_foo);
}


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/


Hope that helps.

All the best,
NwN
Other way? Yes, let the compiler pass the references for you: lambda function syntax.
If bar() is only called by foo(), you could put them in a class together with the vectors as data members.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class FooBar
{
public:
    void foo() {/* do something with v1 and v2; */  bar(); }

private:
    std::vector<int> v1, v2;
    void bar() { /* do something else with v1 and v2 */ }
};

int main()
{
    FooBar fb;
    fb.foo();

    return 0;
}
You're right to try and avoid global variables.

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?
Thanks for all the answers.

@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.
If you're passing by reference, then there should be minimal performance overhead.

Have you actually seen a performance hit, when profiling your 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?
Hi there,

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.

All the best,
NwN
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.

All the best,
NwN
I believe the RV talked about in the article is return values (hence return value optimization), rather than rvalues.

He's talking about both. There's a nice handy title above one of the sections called "Rvalues", if that helps :)

His point is that, when using rvalues, the compiler can safely omit the copying that's usually associated with passing/returning by value.
Apologies, it's been a while since I read the article.
So C++11 move semantics basically answer to that part of his article?

All the best,
NwN
Before move semantics you had compiler optimizations. Now you have both.
Topic archived. No new replies allowed.