Creating a member function, a friend of the class

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;       // This is to be accessed by member function of A

    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?

+4
source share
2 answers

A::accessB friend, . , , A::accessB B:

class A
{
    private:
    int a;

    public:
    void accessB();
};

class B
{
    private:
    int b;

    public:
    friend void A::accessB();
};


void A::accessB()
 {
     B y;
     y.b = 100;
     cout << "Done" << endl;
 }

, a friend , , . , , .

+3

. A::accessB, A . A B, .

+1

All Articles