Home arrow C++ arrow Page 2 - C++ Tricks of the Trade: Friend Functions
C++

C++ Tricks of the Trade: Friend Functions


Friend classes in C++ give us access to non-member functions or other classes. In this article Kais shows us exactly how and why we should use friend classes and functions.

Author Info:
By: Kais Dukes
Rating: 4 stars4 stars4 stars4 stars4 stars / 79
June 21, 2002
TABLE OF CONTENTS:
  1. · C++ Tricks of the Trade: Friend Functions
  2. · Friendly Friends
  3. · Commutative Operators
  4. · Friend Classes
  5. · Conclusion

print this article
SEARCH DEVARTICLES

C++ Tricks of the Trade: Friend Functions - Friendly Friends
(Page 2 of 5 )

By using the friend keyword, a class can grant access to non-member functions or to another class. These friend functions and friend classes are permitted to access private and protected class members. There are places where friends can lead to more intuitive code, and are often needed to correctly implement operator overloading.

If encountering friend functions for the first time, you might feel slightly uneasy since they seem to violate encapsulation. This feeling may stem from the fact that a friend function is not strictly a member of the class.

By thinking of a friend function as part of the class’s public interface, you can get a better understanding of how friends work. From a design perspective, friends can be treated in a similar way to public member functions. The concept of a class interface can be extended from public members to include friend functions and friend classes.

Put another way:

Friend functions do not break encapsulation; instead they naturally extend the encapsulation barrier.

Friend Functions
Friend functions can be declared anywhere within a class declaration, but it is common practice to list friends at the beginning of the class. The public and protected keywords do not apply to friend functions, as the class has no control over the scope of friends.

To see where a friend function would useful, suppose we are constructing a class to represent a rational number as a pair of integers. If we want to use rational numbers in the same way as intrinsic types like a double or an int, then it would be reasonable to be able to write:

CRational r(2, 3);
cout << r << endl;


In order to do this, we need to stream a rational object to an STL ostream by overloading the binary << operator:

class CRational
{
friend ostream &operator<<(ostream &out, const CRational &r);

public:
CRational(const int nNumerator, const int nDenominator);
~CRational();

// ...
};


The friend function gets full access to the members of CRational, in order to do its job of writing to an ostream:

ostream &operator<<(ostream &out, const CRational &r)
{
// Access private members of CRational to display r.
// ...

return out;
}


This operator overload could be declared as a non-friend function, but that would require using get() and set() members to reach the internals of the rational class. It is considered good practice to declare the operator<< function as a friend, even if get/set members exist. When reading through the CRational class declaration it is clear that the operator<< function should be treated as part of the class’s public interface when marked as a friend.

Another reason for using a friend function is one of efficiency: directly accessing data members saves the overhead of using get/set members, if the compiler has not inlined these.
blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials