C + + friend __c++

Source: Internet
Author: User
(1) Concept

(1) Why to make Ufida yuan

Reduce system overhead and improve efficiency when implementing data sharing between classes. If a function in Class A accesses a member of Class B (for example, an implementation of a smart pointer Class), then the function in Class A is a friend function of Class B.

That is, in order for member functions of other classes to access the private variables of the class directly, the outer class or function is allowed to access the class's private and protected variables so that two classes share the same function.

In fact, there are approximately two situations in which you need to use the friend function:
(1) Some occasions where operator overloading requires the use of friends.
(2) Two classes to share data.

Advantages: Can improve efficiency, simple and clear expression;
Disadvantages: friend function broken ring encapsulation mechanism, as far as possible not to use the friend function, unless the case only to make Ufida function;

Because the friend function is a function outside the class, its declaration can be placed in the private segment of the class or the public segment and there is no difference, you can directly adjust the Ufida function, do not need to pass the object or pointer;

(2) friend category

(1) Friend function of ordinary class

Class INTEGER
{
    friend void Print (const integer& obj);//Declare friend function
};

void Print (const integer& obj)
{
}

void Main ()
{
    INTEGER obj;
    Print (obj);//Call Directly
}

(2) Friend Class (all member functions are friend functions)

Class girl;

Class boy
{public
:
void disp (girl &);
The function disp () is a member function of the class boy, and is also a friend function void boy of Class girl
::d ISP (Girl &x)
{
    //with friends, in the boy member function disp, by means of the girl object, Direct access to girl's private variable
    cout<< "Girl ' s name is: <<x.name<<", Age: "<<x.age<<endl;
}

Class Girl
{
private:
char *name;
int age;
Declaring class boy is a friend friend class boy of Class girl
;
(3) Looking at a detailed example
Point.h file

class Point {

private:
    int x, y;

Public: Point
    (int x, int y) {
        this->x = x;
        This->y = y;
    }

    void Getxy ();

    Friend int distance (point &a, point &b);
    Friend class you;
};

Class You {public
:
    double Multi (point &a) {return
        a.x * a.y + 1;
    }
};

void Showpoint ();
Point.cpp file

#include <iostream>
#include "point.h"
using Std::cout;
Using Std::endl;

void Point::getxy () {
    cout << x << ":" << y << Endl;
}

int distance (point &a, point &b) {
    int x = a.x + b.x;
    int y = b.y + b.y;
    return x + y;
}

void Showpoint () {point
    P1 (2, 3), P2 (4, 5);

    P1.getxy ();

    cout << Distance (p1, p2) << Endl;

    you;
    cout << you. Multi (p1) << Endl;
}
(4) Part of operator overload why to make Ufida

Format: Return value operator operator (argument list) {}

Operator overloading does not change the precedence, the number of operands does not change;

When overloading an operator, a function declaration differs within and outside a class, such as +-*/, which requires an operator of 2 operands;

(1) Declaration outside the class:
When declared outside the class, the argument list is 2 arguments, the first argument is the operand to the left of the operator, and the second is the operand to the right of the operator;

ClassType operator+ (classtype& left, classtype& right);

(2) Declare within a class, that is, a member function of a class:

ClassType operator+ (classtype& right);

The first operand is the reference to the object that invokes the operator, and the second operand is the parameter passed in;

So overload << operator, the general wording is:

ostream& operator<< (ostream& os, const classtype& obj);

The first argument is the first operand of the operator, but it is not a class object.
So when the class operator is overloaded and written inside the class, you should declare it as a friend function in order to access other variables or functions within the class except public.

Friend ostream& operator<< (ostream& os, const classtype& obj);
Sample
Std::ostream & operator<< (std::ostream & OS, const time & T) {
    OS << t.hours << "Hours," << t.minutes << "minutes";
    return OS;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.