[C + +] embarrassment ability of 28 friends

The concept of friend

What is friendship?

  • Friend is a relationship in C + +
  • Friendships occur between functions and classes or between classes
  • The relationship of friends is single and cannot be transferred

Usage of friends

  • Declare friends in a class with the friend keyword
  • A friend of a class can be another class or a concrete function
  • Friend is not part of class
  • Friends are not restricted by the access level in the class
  • Friends have direct access to all members of a specific class

Grammar of friends

  • To declare a function or class in a class with the friend keyword

class Point
{
    double x;
    double y;
    
    friend void func(Point& p);
};

void func(Point& p)
{
}

Programming experiment: the use of friend

#include <stdio.h>
#include <math.h>

class Point
{
private:
    double x;
    double y;
public:
    Point(double x, double y)
    {
        this->x = x;
        this->y = y;
    }
    
    double getX()
    {
        return x;
    }
    
    double getY()
    {
        return y;
    }
    
    friend double func_2(Point& p1, Point& p2);
};

// Multiple function calls, slightly inefficient
double func_1(Point& p1, Point& p2)
{
    double ret = 0;
    
    ret = (p2.getY() - p1.getY()) * (p2.getY() - p1.getY()) +
          (p2.getX() - p1.getX()) * (p2.getX() - p1.getX());
          
    ret = sqrt(ret);
    
    return ret;
}

double func_2(Point& p1, Point& p2)
{
    double ret = 0;
    
    ret = (p2.y - p1.y) * (p2.y - p1.y) + 
          (p2.x - p1.x) * (p2.x - p1.x);
          
    ret = sqrt(ret);
    
    return ret;
}

int main()
{
    Point p1(1, 2);
    Point p2(10, 20);
    
    printf("p1(%f, %f)\n", p1.getX(), p1.getY());
    printf("p2(%f, %f)\n", p2.getX(), p2.getY());
    printf("func_1 : |(p1, p2)| = %f\n", func_1(p1, p2));
    printf("func_2 : |(p1, p2)| = %f\n", func_2(p1, p2));

    return 0;
}
Output:
p1(1.000000, 2.000000)
p2(10.000000, 20.000000)
func_1 : |(p1, p2)| = 20.124612
func_2 : |(p1, p2)| = 20.124612

Friend's embarrassment

  • Friend is born to give consideration to the efficiency of C language
  • Friend directly destroys the encapsulation of object-oriented
  • Friend's high efficiency in actual products is not worth the loss
  • Friend has been gradually abandoned in modern software engineering

Matters needing attention

  • Friendship is not transitive
  • A friend of a class can be a member function of another class
  • A friend of a class can be a complete class

    • All member functions are friends

Programming experiment: in-depth analysis of friend

#include <stdio.h>
#include <math.h>

class ClassC
{
private:
    const char* n;
public:
    ClassC(const char* n)
    {
        this->n = n;
    }
    
    friend class ClassB;
};

class ClassB
{
private:
    const char* n;
public:
    ClassB(const char* n)
    {
        this->n = n;
    }
    
    void getClassCName(ClassC& c)        // Pay attention here!
    {
        printf("c.n = %s\n", c.n);
    }
    
    friend class ClassA;
};

class ClassA
{
private:
    const char* n;
public:
    ClassA(const char* n)
    {
        this->n = n;
    }
    
    void getClassBName(ClassB& b)        // Pay attention here!
    {
        printf("b.n = %s\n", b.n);
    }
};

int main()
{
    ClassA A("A");
    ClassB B("B");
    ClassC C("C");
    
    A.getClassBName(B);
    B.getClassCName(C);
    
    return 0;
}
Output:
b.n = B
c.n = C

Summary

  • Friend is born to give consideration to the efficiency of C language
  • Friend directly destroys the encapsulation of object-oriented
  • Friendship is not transitive
  • A friend of a class can be a member function of another class
  • A friend of a class can be a complete class

The above contents refer to the series courses of Ditai Software Institute, please protect the original!

Keywords: C++ Programming C

Added by Jaspal on Fri, 06 Dec 2019 02:13:34 +0200