how to declare a friend function that can access two class

F

freegnu

how to declare a friend function that can access two class
it will look like the following
class A
{
private:
int i;
public:
A(){}
~A(){}
friend void call(A &a, B &b);

};

class A
{
private:
int j;
public:
B();
~B()
friend void call(A &a, B &b);
};

void call(A &a, B &b)
{
cout << a.i << endl << b.j <<endl;
}
 
F

Frank-O

A friend function can be a member of another class :
class A;

class B
{
private :
int j;
public :
B(int j_):j(j_){}

void call(A&);
};


class A
{
private :
int i;
public :
A(int i_):i(i_){}
friend void B::call(A &a);
};


void B::call(A &a)
{

cout << a.i << j << endl;
}

or

class A;

class B
{
private :
int j;
public :
B(int j_):j(j_){}

friend void call(A&,B&b);
};


class A
{
private :
int i;
public :
A(int i_):i(i_){}
friend void call(A &a,B &b);
};


void call(A &a,B &b)
{

cout << a.i << b.j << endl;
}
 
F

freegnu

thanks for your reply
"Frank-O" <[email protected]>
??????:[email protected]...
A friend function can be a member of another class :
class A;

class B
{
private :
int j;
public :
B(int j_):j(j_){}

void call(A&);
};


class A
{
private :
int i;
public :
A(int i_):i(i_){}
friend void B::call(A &a);
};


void B::call(A &a)
{

cout << a.i << j << endl;
}

or

class A;

class B
{
private :
int j;
public :
B(int j_):j(j_){}

friend void call(A&,B&b);
};


class A
{
private :
int i;
public :
A(int i_):i(i_){}
friend void call(A &a,B &b);
};


void call(A &a,B &b)
{

cout << a.i << b.j << endl;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top