Accessing Friend Class

Hello,

Code snippet below:

class A{

public:
void func(void);

}

class B{

public:
friend class A;

private:
void func2(void);

}


void B::func2(void)
{
How do we access the member function func() class A from here ????

}


Kindly advice me on this .
Thanks in advance.

That's not how friends work. A friend just means that you can access the private members/methods of a class when you normally would only have access to the public ones.
yes i understand that , just want to know how do we access in this way ? Is it possible ?
sorry if its wrong.

thanks.
You need an object of type 'A'. You can't call any nonstatic member function without an object:

1
2
3
4
5
void B::func2()
{
  A a;
  a.func();
}
Right, you can directly access private variables from a friend function. If x is a private variable in, say, class friendClass. You can say, within the function:

1
2
3
4
5
void two(friendClass object)
{
    friendClass newObject;
    newObject.x = // whatever
}
Last edited on
Topic archived. No new replies allowed.