In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
, a friend function, that is a "friend" of a given
class
Class or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used differentl ...
, is a function that is given the same access as methods to private and protected
data
In the pursuit of knowledge, data (; ) is a collection of discrete values that convey information, describing quantity, quality, fact, statistics, other basic units of meaning, or simply sequences of symbols that may be further interpreted ...
.
[
]
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 A friend class in C++ can access the private and protected members of the class in which it is declared as a friend. A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main 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
In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.
Namespaces ...
scope may be declared as friend of both classes
*a member function of one class may be declared as friend of another one.
#include
using namespace std;
class Foo; // Forward declaration of class Foo in order for example to compile.
class Bar ;
class Foo ;
// Definition of a member function of Bar; this member is a friend of Foo
void Bar::show(Bar& x, Foo& y)
// Friend for Bar and Foo, definition of global function
void show(Bar& x, Foo& y)
int main()
References
{{reflist
*
The C++ Programming Language
''The C++ Programming Language'' is a computer programming book first published in October 1985. It was the first book to describe the C++ programming language, written by the language's creator, Bjarne Stroustrup. In the absence of an official s ...
by
Bjarne Stroustrup
Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist, most notable for the invention and development of the C++ programming language. As of July 2022, Stroustrup is a professor of Computer Science at Columbia University. ...
External links
C++ friend function tutorialat CoderSource.net
at cplusplus.com
Method (computer programming)
Articles with example C++ code