A friend class can access the private and protected members of other classes that have declared themselves friends. It can be useful to allow one class to access private members of another class.
[sourcecode language="plain"]your code here[/sourcecode]
Like the friend class, this role can access private and protected members of another class. You can also declare a global function as a friend, as shown in the following example.
Important points about Friend class and function:
- Friends should be used for limited purposes only. Too many functions or outer classes declared as friends of a class with protected or private data reduce the value of encapsulating individual classes in object-oriented programming.
- Friendship is not mutual. If class A is friends with B, B will not automatically become friends with A.
- Friendship is not inherited.
- The concept of friends is not in Java.
Key Differences Between Friend Class and Function:
- The friend role is a role that can access private and protected members of a class. In contrast, a friends class is a class that helps access private members of the class.
- The friend function is declared including its prototype in the class and preceded by the friend keyword. Similarly, the friend class is defined by the friend keyword.
- The advance statement is used for friend functions, but does not need to be used in friend classes.
Comparison between Friend Class and Function:
|BASIS FOR COMPARISON
|This is the class used with the friend keyword to access private members of another class.
|This is a function that is used with the friend keyword to allow access of non-member functions to private members of the class.
|I must be used.
|Friend classes can be used when creating a class on top of another class.
|Friend functions can be used in situations of operator overload.
Advantages and disadvantages of friend class and function:
One of the benefits of a friend class or role is that you have access to the class’s private and protected data. For example, if you are implementing a linked list, you can create a linked list class as a friend of the node class and access that data because the linked list is made up of nodes.
Therefore, in certain situations, you may find it advantageous to use a friend class or role. However, it also has some drawbacks.
One of the hallmarks of C ++ is encapsulation. That is, it groups the data with the functions that manipulate it so that external functions and classes cannot access the data. However, allowing one friend class or function to access private members of another class compromises the encapsulation function.
To avoid this, you need to be careful about using friend classes or functions. Your program should avoid using too many friendly classes and functions that would completely compromise encapsulation.
If your role needs access to two or more independent classes, inner members, you need a friend role. On the other hand, if a class needs access to members of another class, it needs a friendly class. If a multi-member function needs to be friends with that function, we recommend using the friend class.
Intern & Technical Content Writer for Programmer’s Academy.
- How To Deal With Massive Tooling Upgrades In Giant Organizations — Smashing Journal
- This AI Analysis Introduces a Deep Studying Mannequin that may Steal Knowledge by Listening to Keystrokes Recorded by a close-by Telephone with 95% Accuracy
- Educating Children Programming – Ok Objects With the Most Sum
- This AI Paper Introduces the Phase Something for NeRF in Excessive High quality (SANeRF-HQ) Framework to Obtain Excessive-High quality 3D Segmentation of Any Object in a Given Scene.