Consider the following program:
#include <iostream>
using namespace std;
class B;
class A {
int a;
public:
A():a(0) { }
void show(A& x, B& y);
};
class B {
private:
int b;
public:
B():b(0) { }
friend void A::show(A& x, B& y);
};
void A::show(A& x, B& y) {
x.a = 10;
cout << "A::a=" << x.a << " B::b=" << y.b;
}
int main() {
A a;
B b;
a.show(a,b);
return 0;
}
What confuses me in this question is that inside class A, we haven't declared function show to be friend function of class A. Then how can we use the value x.a (which is private data member of class A) in the definition of show() function?
According to me, show function would have been friend function of class A, had it been defined in this manner:
friend void show(A& x, B& y);
Please guide.