
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
When should you use 'friend' in C++?
A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.
To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows −
class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); };
To declare all member functions of class ClassTwo as friends of class ClassOne, place the following declaration in the definition of class ClassOne
Example
friend class ClassTwo; For example, #include <iostream> using namespace std; class Box { double width; public: friend void printWidth( Box box ); void setWidth( double wid ); }; // Member function definition void Box::setWidth( double wid ) { width = wid; } // Note: printWidth() is not a member function of any class. void printWidth( Box box ) { /* Because printWidth() is a friend of Box, it can directly access any member of this class */ cout << "Width of box : " << box.width <<endl; } // Main function for the program int main() { Box box; // set box width without member function box.setWidth(10.0); // Use friend function to print the wdith. printWidth( box ); return 0; }
Output
This will give the output −
Width of box: 10
Even though the function was not a member of the class, it could directly access the member variables of that class. This can be very useful in certain situations.
- Related Articles
- When should you use sets in Javascript?
- When you should not use JavaScript Arrow Functions?
- When should you use a class vs a struct in C++?
- When Should I use Selenium Grid?
- How Would You Demonstrate to a Friend That Gases Expands When Heated ?
- When Should You Worry About a Birthmark?
- When should I use MySQL compressed protocol?
- When should I use a composite index in MySQL?
- Why You Should Use an Anonymous Proxy Server?
- When should I use an Inline script and when to use external JavaScript file?
- When You Should Seek Out Medical Attention for Bloating
- What should you absolutely never do when using Python?
- When to use new operator in C++ and when it should not be used?
- Should You Use Exogenous Supplements to Put Your Body in Ketosis?
- When Should You See a Doctor for a Common Cold?
