Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are friends in c++ mutual? [duplicate]

Tags:

c++

syntax

Possible Duplicate:
Friend scope in C++

Are friends in C++ mutual?

like image 248
Liu Avatar asked Oct 13 '10 08:10

Liu


People also ask

Is friendship Mutual in C++?

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.

Who are mutual friends?

Noun. mutual friend (plural mutual friends) Someone who is a friend of two or more people, each of whom may not know each other.

What is friend member in C Plus Plus?

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.

Can a friend be private C++?

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.


2 Answers

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;
};
like image 127
Shay Erlichmen Avatar answered Oct 25 '22 08:10

Shay Erlichmen


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

like image 28
Elemental Avatar answered Oct 25 '22 09:10

Elemental