C++

Friend Classes and Member Friend Functions in C++

979
0
Friend Classes and Member Friend Functions in C++

In C++, there are two ways to give access to private or protected members of a class to another class or function. One way is to use friend classes, and the other is to use member friend functions. Let’s explore each of these concepts in more detail.

Friend Classes

A friend class is a class that is granted access to the private and protected members of another class. To declare a friend class, you must use the keyword friend in the class declaration of the class that is granting access. Here is an example:

class MyClass {
  friend class FriendClass;
private:
  int privateMember;
};
C++

In this example, FriendClass is declared as a friend class of MyClass. This means that FriendClass can access the private member variable privateMember of MyClass.

Note that a friend class declaration only grants access to private and protected members of the class that is being declared as a friend. It does not grant access to private and protected members of any other classes.

Member Friend Functions

A member friend function is a function that is granted access to the private and protected members of a class. To declare a member friend function, you must use the keyword friend in the function declaration of the function that is being granted access. Here is an example:

class MyClass {
  friend int friendFunction(MyClass obj);
private:
  int privateMember;
};

int friendFunction(MyClass obj) {
  return obj.privateMember;
}
C++

In this example, friendFunction is declared as a friend function of MyClass. This means that friendFunction can access the private member variable privateMember of MyClass.

Note that a member friend function declaration only grants access to private and protected members of the class that is declaring the function as a friend. It does not grant access to private and protected members of any other classes.

Using Friend Classes and Member Friend Functions

Friend classes and member friend functions can be useful when you need to give access to private and protected members of a class to another class or function. However, you should use them sparingly, as they can break encapsulation and make your code more difficult to maintain.

Here is an example of how you might use a friend class and a member friend function:

class MyClass {
  friend class FriendClass;
  friend int friendFunction(MyClass obj);
private:
  int privateMember;
};

class FriendClass {
public:
  void doSomething(MyClass obj) {
    obj.privateMember = 42;
  }
};

int friendFunction(MyClass obj) {
  return obj.privateMember;
}
C++

In this example, FriendClass is a friend class of MyClass, and friendFunction is a member friend function of MyClass. This means that FriendClass can access the private member variable privateMember of MyClass and that friendFunction can also access privateMember.

Both FriendClass and friendFunction are modifying or accessing a private member of MyClass, which would otherwise be inaccessible. However, as mentioned earlier, you should use these techniques sparingly and only when necessary.

xalgord
WRITTEN BY

xalgord

Constantly learning & adapting to new technologies. Passionate about solving complex problems with code. #programming #softwareengineering

Leave a Reply