Understanding Friendship in Templates

Take your knowledge of templates to the next level by building a friend function within them.

When we define a class, we can restrict access to its member data and member functions with the protected and private access specifiers. If a member is private, it can only be accessed within the class. If a member is protected, it can be accessed from derived classes with public or protected access. However, a class can grant access to its private or protected members to other functions or classes with the help of the friend keyword. These functions or classes, to which special access has been granted, are called friends. Let’s take a look at a simple example:

Get hands-on with 1400+ tech skills courses.