text:
Friend function
From Wikipedia, the free encyclopedia
Jump to: navigation, search
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[edit]
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 member function of one class may be declared as friend of another one.
#include <iostream> using namespace std; class Foo; // Forward declaration of class Foo in order for example to compile. class Bar { private: int a; public: Bar(): a(0) {} void show(Bar& x, Foo& y); friend void show(Bar& x, Foo& y); // declaration of global friend }; class Foo { private: int b; public: Foo(): b(6) {} friend void show(Bar& x, Foo& y); // declaration of global friend friend void Bar::show(Bar& x, Foo& y); // declaration of friend from other class }; // Definition of a member function of Bar; this member is a friend of Foo void Bar::show(Bar& x, Foo& y) { cout << "Show via function member of Bar" << endl; cout << "Bar::a = " << x.a << endl; cout << "Foo::b = " << y.b << endl; } // Friend for Bar and Foo, definition of global function void show(Bar& x, Foo& y) { cout << "Show via global function" << endl; cout << "Bar::a = " << x.a << endl; cout << "Foo::b = " << y.b << endl; } int main() { Bar a; Foo b; show(a,b); a.show(a,b); }
References[edit]
Jump up ^ 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.)
The C++ Programming Language by Bjarne Stroustrup
External links[edit]
C++ friend function tutorial at CoderSource.net
C++ friendship and inheritance tutorial at cplusplus.com
Retrieved from "https://en.wikipedia.org/w/index.php?title=Friend_function&oldid=794346179"
Categories:
Method (computer programming)
Hidden categories:
Articles with example C++ code
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Article
Talk
Variants
Views
Read
Edit
View history
More
Search
Navigation
Main page
Contents
Featured content
Current events
Random article
Donate to Wikipedia
Wikipedia store
Interaction
Help
About Wikipedia
Community portal
Recent changes
Contact page
Tools
What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Wikidata item
Cite this page
Print/export
Create a book
Download as PDF
Printable version
Languages
Deutsch
Polski
中文
Edit links
This page was last edited on 7 August 2017, at 12:45.
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
Privacy policy
About Wikipedia
Disclaimers
Contact Wikipedia
Developers
Cookie statement
Mobile view
Enable previews

values: mw-page-base noprint

values: mw-head-base noprint

values: firstHeading firstHeading en

values: siteSub noprint

values: jump-to-nav mw-jump

value: b

values:
/wiki/Class_(computer_science)
mw-redirect
Class (computer science)

value: sup

value: p

value: code

value: span

attrs: class id

values:
reflist mw-references-wrap
list-style-type: decimal;

values: Discussion about edits from this IP address [ctrl-alt-n]

values: A list of edits made from this IP address [ctrl-alt-y]

values: You are encouraged to create an account and log in; however, it is not mandatory

values: You're encouraged to log in; however, it's not mandatory. [ctrl-alt-o]

values:
/wiki/Friend_function
View the content page [ctrl-alt-c]
c

values: Discussion about the content page [ctrl-alt-t]

values: p-namespaces navigation vectorTabs p-namespaces-label

values:
p-variants
navigation
vectorMenu emptyPortlet
p-variants-label

values: Past revisions of this page [ctrl-alt-h]

values: p-views navigation vectorTabs p-views-label

values:
p-cactions
navigation
vectorMenu emptyPortlet
p-cactions-label

values:
mw-wiki-logo
/wiki/Main_Page
Visit the main page

values:
/wiki/Main_Page
Visit the main page [ctrl-alt-z]
z

values: Featured content – the best of Wikipedia

values: Find background information on current events

values:
/wiki/Special:Random
Load a random article [ctrl-alt-x]
x

values: portal navigation p-navigation p-navigation-label

values: Guidance on how to use and edit Wikipedia

values: About the project, what you can do, where to find things

values: A list of recent changes in the wiki [ctrl-alt-r]

values: portal navigation p-interaction p-interaction-label

values: List of all English Wikipedia pages containing links to this page [ctrl-alt-j]

values: Recent changes in pages linked from this page [ctrl-alt-k]

values:
/wiki/Wikipedia:File_Upload_Wizard
Upload files [ctrl-alt-u]
u

values: A list of all special pages [ctrl-alt-q]

values: Permanent link to this revision of the page

values: Link to connected data repository item [ctrl-alt-g]

values: Printable version of this page [ctrl-alt-p]

values:
uls-settings-trigger
Language settings
0
button
true

values: /static/images/wikimedia-button-1.5x.png 1.5x, /static/images/wikimedia-button-2x.png 2x

values: /static/images/poweredby_mediawiki_132x47.png 1.5x, /static/images/poweredby_mediawiki_176x62.png 2x

values: mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Friend_function rootpage-Friend_function vector-experimental-print-styles vector-nav-directionality skin-vector action-view