Home c++ Meaning from friendly functions in C++

Meaning from friendly functions in C++




What is the meaning of friendly functions for class and friendly classes for class:

class b;
// Class A Definition
Class A {
 INT N1, N2;
 Friend Void Print_fields (A A); // Practical meaning?
 Friend Class B; // Practical meaning?
 set_fields (n_1, n_2) {
  N1 = n_1;
  N2 = N_2;
Void Print_fields (A A) {
 COUT & LT; & LT; a.n1 & lt; & lt; "" & lt; & lt; a.n2 & lt; & lt; Endl;

Why do it:

friend void print_fields (a a);

If this method is actually a public class member (since it is friendly, then from everyday) and, accordingly, the direct access to the class filling?

This is also not entirely clear:

friend class b;

After all, the class A can be “configured” (protected) and everything will be ok. Please explain.

Answer 1, Authority 100%

Sometimes there is a need to have access to the closed fields of your class data. In this case, no friends do not do.

Here is an example – you wrote a class describing the game unit. This class contains an internal representation M_HP and an open interface injure – Damage, Heal – Treat and Isalive – Check if the unit is alive.

class unit
  Friend void testunit ();
  Unit (Unsigned Maxhp)
  , M_HP (MAXHP) {};
  Void Injure (unsigned val) {
    M_HP - = VAL;
    if (m_hp & lt; 0) {m_hp = 0; }
  Void Heal (unsigned val) {
    M_HP + = VAL;
    if (M_HP & GT; M_MAXHP) {M_HP = M_MAXHP; }
  BOOL ISALIVE () const {return M_HP & GT; 0; }
  unsigned m_maxhp;
  int m_hp;

You decide to wrap the functionality of the class of the Yunit tests, to be sure that the void injure (unsigned val) and void head (unsigned val) work correctly:

void testunit ()
  Unit Unit (100);
  unit.injure (10);
  Assert (unit.m_hp == 90); // Check Private Data Field
  Unit.Heal (20);
  assert (unit.m_hp == 100); // Check Private Data Field
  Testunit ();

As you can see, the test of the test would be impossible without contacting the internal presentation of the class. For this purpose, we have announced the function void testunit () Class Friendly unit


Another example of using friendly functions when designing your own class and ensuring its functionality. Below is a very simple user class wrapping an integer. To place the instances of this class in the output stream, the most natural way to determine the function Operator & lt; & lt; Not instance of class

# include & lt; iostream & gt;
class wrappedint
  Explicit Wrappedint (Int Val)
  Friend Std :: Ostream & Amp; Operator & lt; & lt; (std :: ostream & amp; os, const wrappedint & amp; val);
std :: ostream & amp; Operator & lt; & lt; (std :: ostream & amp; os, const wrappedint & amp; val)
  OS & LT; & LT; val.m_value;
  Return OS;

And in order for this function to have access to closed class fields, such as M_VALUE, we make it friendly in relation to the class wrappedint.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions