Friend function
In object-oriented programming, a friend function, that is a "friend" of a given class, is a function that is given the same access as methods to private and protected data.[1]
A friend function is declared by the class that is granting access, so friend functions are part of the class interface, like methods. Friend functions allow alternative syntax to use objects, for instance f(x) instead of x.f(), or g(x, y) instead of x.g(y). Friend functions have the same implications on encapsulation as methods.
A similar concept is that of friend class.
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:
- A function of global or namespace scope may be declared as friend of both classes.
- A method of one class may be declared as friend of another one.
// C++ implementation of friend functions.
import std;
// Forward declaration of class Foo in order for example to compile.
class Foo;
class Bar {
private:
int a = 0;
public:
// Definition of a method of Bar; this is a friend of Foo
void show(Bar& x, Foo& y) {
std::println("Show via function member of Bar");
std::println("Bar::a = {}", x.a);
std::println("Foo::b = {}", y.b);
}
friend void show(Bar& x, Foo& y); // declaration of global function as friend
};
class Foo {
private:
int b = 6;
public:
friend void show(Bar& x, Foo& y); // declaration of global function as friend
friend void Bar::show(Bar& x, Foo& y); // declaration of friend from other class
};
// Friend for Bar and Foo, definition of global function
void show(Bar& x, Foo& y) {
std::println("Show via global function");
std::println("Bar::a = {}", x.a);
std::println("Foo::b = {}", y.b);
}
int main() {
Bar a;
Foo b;
show(a, b);
a.show(a, b);
}
References
- ^ Holzner, Steven (2001). C++ : Black Book. Scottsdale, Ariz.: Coriolis Group. p. 397. ISBN 1-57610-777-9.
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.)
External links
- C++ friend function tutorial at CoderSource.net
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.