A C++ friend functions are special functions which can access the - TopicsExpress



          

A C++ friend functions are special functions which can access the private members of a class. They are considered to be a loophole in the Object Oriented Programming concepts, but logical use of them can make them useful in certain cases. For instance: when it is not possible to implement some function, without making private members accessible in them. This situation arises mostly in case of operator overloading. In the following example, the friend function print is a member of class TWO and accesses the private data members a and b of class ONE. #include using namespace std; //Must be known to TWO //before declaration of ONE. class ONE; class TWO { public: void print(ONE& x); }; class ONE { int a, b; friend void TWO::print(ONE& x); public: ONE() : a(1), b(2) { } }; void TWO::print(ONE& x) { cout
Posted on: Mon, 16 Sep 2013 09:46:32 +0000

Trending Topics



Recently Viewed Topics




© 2015