Home

 › 

Articles

 › 

Friend Function and Friend Class In C++, Explained

Friend Function and Friend Class in C++

Friend Function and Friend Class In C++, Explained

In the programming world, there are a lot of choices of languages to use. Many of these are known as object-oriented programming languages, which focus on designing programs around objects and how they interact with each other.

To this end, the properties of objects are defined by classes and are used to contain data as well as code for operating on that data.

A popular example of an object-oriented language is C++, and much of its usage involves the friend function and friend classes. In this article, we’re going to get into what a friend function and a friend class are, and how they’re used with C++.

What Is the Friend Function and Class?

Hiding, or encapsulating, data is a key principle within object-oriented programming, usually in order to provide a more user-friendly interface to interact with, or restrict access of, functions to certain blocks of code. However, sometimes you may want code within a certain class to be accessed by a function outside of it.

This is where the friend keyword and friend function come into play. A friend function can access protected and private data and can be declared by using the friend keyword. As such, the friend function can be used to operate on objects of one or more classes.

It’s worth noting a friend function can be either a member function (associated with a particular class) or a global function (defined outside of any class).

Friend Function and Friend Class in C++
Friend functions are normal external functions that are given special access privileges.

A friend class functions in a similar way, but, since the entire class is a “friend” of another class, all the member functions of the friend class have access to all the members of the other class.

In other words, a friend function is declared as a friend of a particular class, whereas a friend class is defined within the class you want it to be friends with.

How to Use the Friend Function

Now that we’ve covered what friend functions and classes are, it’s time to look at an example of how the friend function can be implemented:

class MyClass {
  private:
   int x;
  public:
    friend void myFriendFunction(MyClass& obj) {
     obj.x = 10;
    }
}

Here, we’ve defined the “myFriendFunction” function as a friend of the class “MyClass”. As such, the function is declared inside the class definition and can access the private member of this class, “x”.

How to Use a Global Friend Function

If we want to declare the friend function as a global function, we have to define it outside of the class definition. This can be done very similarly to the previous example:

class MyClass {
private:
   int x;
public:
   friend void my GlobalFriendFunction(MyClass& obj);
};

void myGlobalFriendFunction(MyClass& obj) {
  obj.x = 10;
}

The code is essentially the same, except that the friend function is defined outside of the class. As such, this friend function can potentially be a friend of any class, since it hasn’t been defined inside a class.

How to Declare a Function as a Friend of Multiple Classes

You don’t just have to stop at making a function friendly to one class. Functions can be friends with multiple classes. See how to do this below:

class MyClass1;

class MyClass2 {
private:
  int y;
public:
  friend void myFriendFunction(MyClass1&, MyClass2&);
};

class MyClass1 {
private:
  int x;
public:
  friend void myFriendFunction(MyClass1&, MyClass2&);
};

void myFriendFunction(MyClass1& obj1, MyClass2& obj2) {
  obj1.x = 10;
  obj2.y = 20;
}

Here, we’ve defined two classes, “MyClass1” and “MyClass2”, as well as the “myFriendFunction” global function. The friend function is declared within both class definitions, and can, therefore, modify the private members of both these objects. Namely, members “x” and “y”. Note that the first line is used to forward declare “MyClass1.”

Friend Function and Friend Class in C++
A friend class can access the private and protected members of the class in which it is declared a friend.

This lets the compiler know about the existence of “MyClass1” without defining it first. This is done so that “MyClass1” is considered when compiling the declaration of the friend function, avoiding compilation errors.

How to Use a Friend Class

In the previous example, we defined the “myFriendFunction” as a friend of the “MyClass” class. Access to and modification of the private member “x” is granted to the friend class.

However, a friend class looks a little different. Here is an example:

class MyClass {
 private:
   int x;
 public:
  friend class MyFriendClass;
};

class MyFriendClass {
 public:
  void myFunction(MyClass& obj) {
   obj.x = 10;
  }
}

In this case, the class “MyFriendClass” is designated as a friend class of the “MyClass” class. Every function within can access the private member “x,” such as the “myFunction” member function.

Wrapping Up

The friend keyword can be used to declare a friend function. This lets the function access private members of a class. You can also use the friend keyword to declare a friend class. This means that all member functions of this class can access the friendly class.

The friend keyword is useful when we want to grant access to specific members of a class to a function or class without revealing all of these protected members to the public interface. In general, the function should be used only when necessary. This is to help maintain the data-hiding and encapsulation principles of object-oriented programming.

Up Next

Frequently Asked Questions

What is a friend function?

A friend function is a function that’s been granted access to the private members of a class using the friend keyword.

What is a friend class?

A friend class functions similarly, except that all member functions of the friend class can access the other class’s protected members.

Is a friend function a member or global function?

It can be either. If you define the friend function outside of the class, it’s a global function and can potentially access other classes. If it’s defined within the class definition, it can only access members of that class.

Why would you use a friend function or class?

They’re useful for granting access to private or protected class members, without exposing all of this information to the public interface. This is important for maintaining data hiding and encapsulation.

Can a friend function access members of a subclass?

No, if it’s defined within a base class then it can’t access members of subclasses.

Does the friend function affect performance?

No, the friend function only affects the visibility of private class members.

Can a friend class be inherited?

Yes, friend classes can be inherited.

To top