Content deleted Content added
m Reverted 1 edit by 107.77.189.55 (talk) to last revision by WOSlinker |
→Use cases: Fixed typos and mentioned that the code is in C++. |
||
Line 1:
In [[object-oriented programming]], a '''friend function''', that is a "friend" of a given [[class (computer science)|class]], is a function that is given the same access as methods to private and protected [[data]].<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>
Line 9 ⟶ 8:
==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:
*
*
<syntaxhighlight lang=
// 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:
|