I tried to write code to implement a member function that could access the personal data of a class by declaring it as a friend in the class. But my code does not work, and I can not understand what is wrong with it:
#include <iostream>
using namespace std;
class A;
class B
{
private:
int b;
public:
friend void A::accessB();
};
class A
{
private:
int a;
public:
void accessB();
};
void A::accessB()
{
B y;
y.b = 100;
cout << "Done" << endl;
}
int main(void)
{
A x;
x.accessB();
}
I am trying to access B private content using the getAccessB function , which is a member function of A. I declared it as a friend. What is wrong with that?
source
share