Entry
C++: Class: Friend: Can someone tell me what a friend class is in C++?
Sep 25th, 2003 07:05
Knud van Eeden, Colin Thomsen,
Friend classes
A friend class in C++ allows one class to access all the private and
protected member variables and methods in another class. For example:
class A
{
friend class B;
private:
int v1;
};
class B
{
public:
void initA()
{
A some_a;
// Here we access the private member variable v1
// in A as if it were public
some_a.v1 = 10;
}
};
This might be useful for two tightly coupled classes (data and
processor for example) where you don't want to provide a public
function to set v1. It can also be useful for testing.
Note that if you have class A and class B defined in separate header
files, you don't need to forward declare or #include the class B to
declare that it is a friend of class A.
----------------------------------------------------------------------
--- Knud van Eeden --- 30 April 2021 - 07:45 pm ----------------------
Use the keyword 'friend'.
---
Friend functions
It is possible to grant a non-member function access to the 'private'
members
of a class by using a 'friend'.
A 'friend' function has access to all 'private' and 'protected'
members
of the
class for which it is a 'friend'.
To declare a 'friend' function, include its prototype within the class,
preceding it with the C++ keyword 'friend'.
---
Consider this C++ program:
// save as FriendShowExample.cpp
//
#include <iostream.h>
//
class CLASSFriendShowExample {
private:
int xI;
int yI;
public:
void PROCMathSumSet( int iI, int jI );
friend int FNMathSumI( CLASSFriendShowExample xO );
};
//
void CLASSFriendShowExample::PROCMathSumSet( int iI, int jI ) {
xI = iI;
yI = jI;
}
//
int FNMathSumI( CLASSFriendShowExample xO ) {
// note: FNMathSumI() is not a member function of any class
// because FNMathSumI() is a friend of CLASSFriendShowExample
// it can directly access its private variables like xI and yI.
return( xO.xI + xO.yI );
}
//
void main() {
CLASSFriendShowExample nO;
nO.PROCMathSumSet( 3, 4 );
cout << "sum of the 2 given numbers is " << FNMathSumI( nO ) << endl;
}
//
// if you run this you will see:
// c:\borland\bcc55\bin\bcc32.exe -Ic:\borland\bcc55\include -
Lc:\borland\bcc55\lib
// c:\bbc\taal\FriendShowExample.cpp
// Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
// c:\bbc\taal\FriendShowExample.CPP:
// Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
// FriendShowExample.exe
// sum of the 2 given numbers is 7
// Press any key when ready...
---
Although there is nothing gained by making FNMathSumI a 'friend' rather
than a member function of CLASSFriendShowExample, there are some
circumstances in which 'friend' functions are quite valuable.
First, 'friend' functions can be useful when you are overloading
certain
types of operators.
Second, 'friend' functions make the creation of some types of
input/output
functions easier.
Third, 'friend' functions may be desirable in that in some cases, two
or
more classes may contain members that are interrelated relative to
other
parts of your program.
[book: source: Schildt, Herbert - C++, the complete reference / 2th
edition - McGraw-Hill - ISBN 0-07-882123-1 - year: 1995 - p.
286 'friend functions']
Internet: see also:
C#: Class: Friend: Can someone tell me what a friend class is in C#?
http://www.faqts.com/knowledge_base/view.phtml/aid/24737/fid/791
Delphi: Class: Friend: Can someone tell me what friend class is in
Object Pascal?
http://www.faqts.com/knowledge_base/view.phtml/aid/16168/fid/175
Java: Class: Friend: Can someone tell me what friend class is in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/16166/fid/165
----------------------------------------------------------------------