Possible Duplicate:
Friend scope in C++
Are friends in C++ mutual?
Proceeding normally to declare and define functions within both of these classes, there will be a common syntax error is encountered, which signals that the class declared first cannot access the data members of the succeeding class even when made a friend. Therefore Friendship is not mutual.
Noun. mutual friend (plural mutual friends) Someone who is a friend of two or more people, each of whom may not know each other.
A friend function is a special function in C++ which in-spite of not being member function of a class has privilege to access private and protected data of a class. A friend function is a non member function or ordinary function of a class, which is declared as a friend using the keyword “friend” inside the class.
A friend function is a function that isn't a member of a class but has access to the class's private and protected members.
class bar
{
private:
void barMe();
};
class foo
{
private:
void fooMe();
friend bar;
};
In the above example foo class can't call barMe() You need to define the classes this way in order that the friend be mutual:
class foo; // forward
class bar
{
private:
void barMe();
friend foo;
};
class foo
{
private:
void fooMe();
friend bar;
};
The friend relationship is only one-way in general - but there is nothing to stop you declaring Class A a friend of class B AND class B a friend of class A. So a mutual relationship can be established
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With