C + + friend __c++

Source: Internet
Author: User
Tags class definition

Compiler uses mingw32-g++ c++11, editor for Codeblocks
If a class wants other classes or functions to be able to access non-public members of the class, then you can use the friend method.

friend function:
If you want a non-member function to manipulate a private member of a class, you can declare the function in this and the friend keyword in the previous table of the function. In general, it is a good idea to declare friends in a place where the class definition begins or ends.

Class A
{
    friend void fun (a &a);
Public:
    string member2;
Private:
    int member1;
};
void Fun (a &a)//This function can use the private member {a.member1=0 in class A
    ;
}

Friend class:
The simple point is that a class can use a private member of another class. If you want to use a private member of Class B in Class A, you need to declare friend Class A in class B;
At the same time, it should be noted that the friend relationship does not have a transitive nature.

Class B
{
    friend class A;
Private:
    int member1;
};
Class A
{public
:
    string member2;
    void Fun (B &b)
    {
        b.member1++;
    }
Private:
    int member1;
};

member functions in a class as friends:
When you declare a member function Chengyuan, you must explicitly indicate which class the member function belongs to. For example, in Class A another member function fun () to use private members in class B, you need to follow these steps.
1. First forward declaration Class B
2. Define Class A, declare the fun () function in Class A, but cannot define the fun () function
3. Then define Class B and declare the friend function in Class A
4. Define Fun function

Class B;
Class A
{public
:
    string member2;
    void Fun (B &b);
Private:
    int member1;
};
Class B
{
    friend void A::fun (b &b);
Private:
    int member1;
};
void A::fun (b& B)
{
    b.member1++;
}

Declaration and scope of friends:
A friend function can declare a + definition directly in a class.

Class B
{
    friend void F (B &b)
    {
        b.m2=0
    }
Public:
    void get_m2 () {Cout<<this->m2<<endl}
    string M1;
Private:
    int m2;
};
int main ()
{
    Ios::sync_with_stdio (false);
    b b;
    f (b);
    B.GET_M2 ();
    return 0;
}

To call fun in a class, you must declare it, or you will get an error.

Class b//will complain
{
    friend void F (B &b)
    {
        b.m2=0;
    }
Public:
    void get_m2 () {Cout<<this->m2<<endl}
    string M1;
    B ()
    {
        f ();
    }
Private:
    int m2;
};


void f ();//Add Declaration
class B
{
    friend void F (b &b)
    {
        b.m2=0;
    }
Public:
    void get_m2 () {Cout<<this->m2<<endl}
    string M1;
    B ()
    {
        f ();
    }
Private:
    int m2;
};

Note here that some compilers do not enforce the qualifying rules for appeals on friends. (as the book says)

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.