class b;
class a
{
friendclass b;
public:
a() {};
void seta()
{
i=10;
}
private:
int i;
};
class b
{
friendclass a;
b() {};
void setb()
{
i=23;
a::i=23;
seta();
}
private:
int ii;
};
Compiling this snippet with g++ i haven’t access to members...
Compiler says ....
i=23;//error: ‘i’ was not declared in this scope
a::i=23;//error: invalid use of non-static data member ‘a::i’
seta();//error: ‘seta’ was not declared in this scope
class B ;
class A
{
private: int ia = 0 ;
friend B ; // B is a friend of A
};
class B
{
public:
void set( A& a2 )
{
ib = 123 ;
a.ia = 4567 ; // friend: can access private member of A
a2.ia += a.ia ; // friend: can access private member of A
}
private:
A a ;
int ib = 0 ;
};