Friend function: Difference between revisions

Content deleted Content added
Some copyediting
m Use cases: consistently "method" (already three times in the intro and also category)
 
(36 intermediate revisions by 25 users not shown)
Line 1:
In [[object-oriented programming]], a '''friend function''', that is a "friend" of a given [[class (computer science)|class]], is alloweda function that is given the same access toas methods to <code>private</code> and <code>protected</code> [[data]].<ref>{{cite inbook|last=Holzner|first=Steven|title=C++ that: classBlack thatBook|year=2001|publisher=Coriolis itGroup|___location=Scottsdale, wouldAriz.|isbn=1-57610-777-9|page=397|quote=When notyou normallydeclare bea ablefunction toa asfriend ifof a class, that function has access to the internal data wasmembers <code>public</code>of that object (that is, its protected, and private data members.)}}
<ref>{{cite book|last=Holzner|first=Steven|title=C++ : Black Book|year=2001|publisher=Coriolis Group|___location=Scottsdale, Ariz.|isbn=1-57610-777-9|page=397|quote=When you declare a function a friend of a class, that function has access to the internal data members of that object (that is, its protected, and private data members.)}}
</ref>
Normally, a [[function (computer science)|function]] that is defined outside of a class cannot access such information. Declaring a function a '''friend''' of a class allows this, in languages where the concept is supported.
 
A friend function is declared by the class that is granting access, explicitlyso statingfriend whatfunctions functionare frompart aof the class isinterface, allowedlike accessmethods. AFriend similarfunctions conceptallow isalternative thatsyntax to use objects, for instance <code>f(x)</code> instead of <code>x.f()</code>, or <code>g(x,y)</code> instead of <code>x.g(y)</code>. Friend functions have the same implications on [[friendseparation classof concerns|encapsulation]] as methods.
 
A similar concept is that of [[friend class]].
Too many functions or external classes declared as friends of a class with protected or private (visibility modes) data may lessen the value of [[separation of concerns|encapsulation]] of separate classes in object-oriented programming and may indicate a problem in the overall architecture design. It may also lead to tight coupling of classes.
 
==Use cases==
This approach may be used in friendly function when a function needs to access private data in objects from two different classes.
This may be accomplished in two similar ways:
*aA function of global or [[namespace]] scope may be declared as friend of both classes.
*aA member functionmethod of one class may be declared as friend of another one.
<sourcesyntaxhighlight lang=Cpp"cpp">
// C++ implementation of friend functions.
#include <iostream>
using namespace std;
class Foo; // Forward declaration of class Foo in order for example to compile.
 
class Bar {
private:
int a = 0;
public:
Bar(): a(0) {}
void show(Bar& x, Foo& y);
friend void show(Bar& x, Foo& y); // declaration of global function as friend
};
class Foo {
private:
int b = 6;
public:
friend void Bar::show(Bar& x, Foo& y); // declaration of friendglobal fromfunction other classas friend
Foo(): b(6) {}
friend void Bar::show(Bar& x, Foo& y); // declaration of global friend from other class
friend void Bar::show(Bar& x, Foo& y); // declaration of friend from other class
};
// Definition of a member functionmethod of Bar; this member is a friend of Foo
void Bar::show(Bar& x, Foo& y) {
cout << "Show via function member of Bar" << endl;
Line 57 ⟶ 55:
a.show(a,b);
}
</syntaxhighlight>
</source>
 
==References==
Line 65 ⟶ 63:
==External links==
*[http://www.codersource.net/c/ctutorials/ctutorialfriend.aspx C++ friend function tutorial] at CoderSource.net
*[http://www.cplusplus.com/doc/tutorial/inheritance.html C++ friendship and inheritance tutorial] at cplusplus.com
 
[[Category:Method (computer programming)]]