C + + friend __c++

Source: Internet
Author: User
friend Yuan

friend English friend. Friend translated into Chinese is friends , translation into the term is: friends . Friends are very good understanding, my money you casually spend, my things you casually use; Of course I am your friend, your money I casually spend, your things I casually use.

Of course in C + + , classes and Classes can be friends , then this class can be used to manipulate the private members of another class, the function and function can also be used as friends , So friends are divided into: friend function and friend class .

We can see by an example: Write code

#include <iostream>
#include <string>

using namespace std;

Class screen{public
:
    typedef std::string::size_type Index;

    screen (int ht=0, int wd=0): Contents (HT*WD, "), cursor (0), height (HT), Width (wd) {}

    int area () const{return
        Heig HT * WIDTH;
    }
Private:
    std::string contents; 
    index cursor;
    int height, width;



int main () {screen
    A;
    cout << A.area () << Endl;
    cout << "OK" << Endl;

    return 0;
}

Run the current program, the program can be used normally.

Now, we write a function at the same level as the main () function, which is to compute the area of the screen.

This function is not a member function of the class
int CalcArea (screen & screens) {return
    screen.height * screen.width;
}

If this function is written like this, the compiler will not pass the program. Because height and width are private members of the screen class. These two private data members can only be used inside a class and can only be used by internal functions of a class, because the CalcArea () function is not a member function inside a class, so it cannot use private members within a class.
So if, now, we turn this calcArea () function into a friend of the screen class, that is, friends, then this function can use the private members of the class. It's like: You Are my friend, come to my house, just like to your own home, eat casually use casually take.

Now we're going to define the friend function. We define the CalcArea () function as a friend function.

Inside the screen class, the following code is added to public. It's as simple as this:

    Friend int CalcArea (screen & screen);

Now that we're compiling the program, there's no mistake. There is no problem with running. The main () function is written in the following way to test:

int main () {screen
    A;
    cout << A.area () << Endl;
    cout << CalcArea (a) << Endl;
    cout << "OK" << Endl;
    while (1) {} return
    0;
}

We can also write a class as a friend, so let's take a look at the following example: ( Note: to put the following code below the screen class, the Window_mgr class cannot find the screen class.) )

Window Management class-manage class
window_mgr{public
:
    //Reposition-is to change the height and width void relocate of the window
    (int r, int C, screen& s) {
        s.height + = r;
        S.width + C;
    }
;

Because the height and width in the screen class is private, it cannot be used in the Window_mgr class unless it is a friend.
The compiler now has an error.
We can now use the Window_mgr class as a friend of the screen class. What needs to be done is just: add the following code to public inside the screen class:

    Friend class Window_mgr;

Now, we will window_mgr this whole class as a friend of the screen class, that is to say: they are friends now, our family can come to your home casually, your family can come to our home.

Let's test it in the main () function:

int main () {screen
    a (a);
    cout << A.area () << Endl;

    Window_mgr W;
    W.relocate (a);
    cout << CalcArea (a) << Endl;

    cout << "OK" << Endl;
    while (1) {} return
    0;
}
This is friends, you understand.

If we can't get this window_mgr class to be a friend of the screen class now, we want just a member function in the Window_mgr class to be a friend of the screen class. How to operate it.

We are defining a class, Dog. There are many member functions within the dog class.

I don't want to make the dog class all the friends of the screen class, and I just want to use the Foo () in the dog class as a friend of the screen class, so what do we do now?
Quite simply, we add the following code to the public of the screen class:

    Friend int Dog::foo (screen &);

Q: Now when the program compiles, this is why. A : There is one problem to emphasize: there is a dependency between the friend definition and the friend declaration.

If we put the Dog class underneath the screen class, then the Dog class can find the screen class, but the screen class cannot find the Foo () function inside the Dog class, because we wrote the Foo () function in the form of declarations and definitions in one.
If we put the Dog class on top of the screen class, the screen class can find the Foo () function in the Dog class, but the Dog class is now unable to find the screen class. So, no matter where you put the Dog class in the program, there is no way to compile it. How to solve this problem.

We need to write this on the screen class:

class screen;

Class dog{public
:
    int foo (screens & screens);

    int Koo (screen & screens) {return
        1;
    }
};

Declare the screen class first, and then define the Dog class. And in the Dog class we write the Foo () function in the form of a declaration. This does not use the members of the screen class, so that the program does not need to look up the definition of screen when compiling the code.

Next, we write the following in the screen class:

int Dog::foo (screen & screens) {
    return  screen.height * screen.width;
}

We define the member function foo () function in the class below in the screen class Dog. The reason for this is: because the Foo () member function uses the member variables defined in the screen class, the program will go up and look up the definition of the screens class when compiling the code for this function, just as we write the definition of the Foo () function underneath the definition So write so that you can use a compile.

Now write some test code in the main () function:

int main () {screen
    a (a);
    cout << A.area () << Endl;

    Dog D;
    cout << D.foo (a) << Endl;

    cout << "OK" << Endl;
    while (1) {} return
    0;
}

Now we have all introduced the friend Yuan .

Finally, the complete code is posted:

#include <iostream> #include <string> using namespace std;

Class screen;

    Class dog{public:int foo (screen & screen);
    int Koo (screen & screens) {return 1;


}
};
    Class screen{Public:friend int calcArea (screen & screen);
    Friend class Window_mgr;

    Friend int Dog::foo (screen & screen);

    typedef STD::STRING::SIZE_TYPE Index;
        screen (int ht=0, int wd=0): Contents (HT*WD, "), cursor (0), height (HT), Width (wd) {} int area () const{
    return height * width; 
    } private:std::string contents;
    index cursor;
int height, width;

}; Window Management class-Manage Class window_mgr{public://reposition-is to change the height and width void relocate of the window (int r, int c, scree
        n& s) {s.height + = r;
    S.width + = C;

}
};


int Dog::foo (screen & screens) {return screen.height * screen.width;}

This function is not a member function of the class int CalcArea (screen & screens) {return screen.height * screen.width;} int mAin () {screen A (60, 100);

    cout << A.area () << Endl;
    Window_mgr W;
    W.relocate (a);

    cout << CalcArea (a) << Endl;
    Dog D;

    cout << D.foo (a) << Endl;
    cout << "OK" << Endl;
while (1) {} return 0;
 }

Summary:
1. Today we study is friend Yuan, friend Yuan is to find friends. We can use a function as a friend, we can also use a class as a friend, or a member function in a class as a friend.
2. There are two types of all friend functions: normal functions and member functions of classes (both common and private).
3. Friend, the truth is very simple. After you have friends, you can manipulate all private data members and private functions.

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.