Professional Documents
Culture Documents
Pi19404
November 22, 2013
Contents
Contents
C++ Class Members and friends
0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0.2 Friend Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
3 3
2|9
3|9
class F; class X; void print2(); void print1(X x1); class Y { public: void print(X& x); }; class X { private: int a; protected: int c; friend void friend void friend void { cout << }
Y::print(X& x); print1(X x); print2() "defining a friend function in the class " <<endl;
public: X() : a(1), c(2) { } }; void print1(X x1) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x1.a << endl; cout << "c is " << x1.c << endl; } void Y::print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl; cout << "c is " << x.c << endl;
int main() {
4|9
If a class F is a friend of class A,then every member function and static data member definition of class F has access to class A. For using the class as a friend,in the declaration must be elaborated class name , for example friend class F A friend class can be defined after it is declared as friend in other class A friend cannot be declared with a storage class specifier. A class cannot be defined within a friend declaration,
#include <iostream> #include <typeinfo> #include <stdlib.h> using namespace std; class F; class X; class X { private: int a; protected: int c; friend class F; public: X() : a(1), c(2) { } }; class F { public: void print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl;
5|9
} };
class Z1 { private : int z1; public: Z1() : z1(4){} friend void print3(Z &z) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; //cout << "a is " << z.z2 << endl; //including this line will give comp cout << "b is " << z.z11.z1 << endl; } }; Z1 z11; private : int z2; public: Z() : z2(3){} };
Friends of a base class are inherited by any classes derived from that base class The functionality that the friend of the base
6|9
C++ Class Members and friends class are inherited by the derived by the base class may be compiler dependent.The g++ compiler on Linux supports this feature,while many online references suggest this inheritance does not hold.
class X { private: int a; protected: int c; friend class F; public: X() : a(1), c(2) { } }; class X1 : private X { }; class F { public: void print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl; cout << "c is " << x.c << endl; } void print(X1 & x1) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x1.a << endl; cout << "c is " << x1.c << endl; } }; int main() { X xobj; fobj.print(xobj); fobj.print(x1obj); }
If a class is derived from a friend class ,then it is also a friend of
7|9
C++ Class Members and friends the original base class.This feature may also be compiler dependent.The g++ compiler on Linux supports this feature,while many online references suggest this inheritance does not hold.
class X { private: int a; protected: int c; friend class F; public: X() : a(1), c(2) { } }; class F { public: void print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl; cout << "c is " << x.c << endl; } } class G : public F { } void main() { X xobj; G gobj; gobj.print(X); }
Friendships are not transitive if A is a friend of B,and C is a friend of A,it does not imply that C is a friend of B unless explicitly specified.
8|9
public: X() : a(1), c(2) { } }; class F { public: void print(X& x) { cout << "In member cout << "a is " << cout << "c is " << } } class H { public: void print(X& x) { cout << "In member cout << "a is " << cout << "c is " << } };
function " << __PRETTY_FUNCTION__ <<endl; x.a << endl; x.c << endl;
function " << __PRETTY_FUNCTION__ <<endl; x.a << endl; x.c << endl;
class F { public: void print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl; cout << "c is " << x.c << endl; } void print(X1 & x1) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x1.a << endl; cout << "c is " << x1.c << endl; } friend class H; }; int main() { X xobj; H hobj; // hobj.print(xobj); // this line will give compilation error }
9|9