C + + friend detailed

Source: Internet
Author: User

1. What is Friend
In a Class A, Class B is declared as a friend class, and Class B can access the private and protected members of Class A. Alternatively, you can declare a function as a friend function.

2. When to use friend
If there are some shared data members between different classes, you can simplify the design of the class by making the UF meta.

3. Friend class
Friend class declaration: Friend classes Name
Examples are as follows:
For TV and remote control, sharing channel data members and switch operation, the remote control can be declared as a friend of the TV class, the remote control can directly access the TV data members, and a remote control can be controlled more than one TV , this example is a good embodiment of the characteristics of friends.

Tv.h

#ifndef Tv_h_#defineTv_h_classtv{ Public: FriendclassRemote; enumState{off,on}; Tv (intS=off,intMc= the): State (s), Maxchannel (MC), channel (2){}    voidOnOff () {state= (state==on)?Off:on;} voidChanup (); voidChandown (); voidSettings ()Const;Private:    intState ; intMaxchannel; intChannel;};classremote{ Public: Remote () {}; voidOnOff (Tv &t)    {T.onoff ();}; voidChanup (Tv &6)    {T.chanup ();}; voidChandown (Tv &t)    {T.chandown ();}; voidSet_chan (Tv &t,intc) {t.channel=c;};};

Tv.cpp

#include <iostream>#include"tv.h"voidTv::chanup () {if(channel<Maxchannel) {Channel++; }    Else{Channel=1; }}voidTv::chandown () {if(channel>1) {Channel--; }    Else{Channel=Maxchannel; }}voidTv::settings ()Const{    usingstd::cout; usingStd::endl; cout<<"Tv is"<< (State==on?" on":"off") <<Endl; if(state==On ) {cout<<"Channel:"<<channel<<Endl; }}

Use_tv.cpp can control multiple different TVs using the same remote controller

#include <iostream>#include"tv.h"intMain () {usingstd::cout; usingStd::endl;    Tv S42; cout<<"initing Tv s42 ..."<<Endl;    S42.settings ();    S42.onoff ();    S42.chanup (); cout<<"adjusted Tv s42 ....."<<Endl;    S42.settings ();    Remote control; Control.set_chan (S42,Ten);    Control.volup (S42); cout<<"settings after using remote ..."<<Endl;    S42.settings ();    GetChar (); return 0;}

As you can see, if you do not use friends, you must set the private part of the TV class to common, or create a large class to contain the TV and remote. This workaround also fails to respond, and a remote control can be used for multiple TVs.

4. Friend function
As can be seen from the above example, only the Set_chan method of the remote class uses the private members of the TV class, so we can make Remote::set_chan () A friend function of the TV class without having to make the whole class A friend .

Tvfm.h

#ifndef Tv_h_#defineTv_h_classTv;classremote{ Public :    enumState{off,on};Private:    intmode; Public: Remote () {}; voidOnOff (Tv &t); voidChanup (Tv &t); voidChandown (Tv &t); voidSet_chan (Tv &t,intc);};classtv{ Public: FriendvoidRemote::set_chan (Tv &t,intc); enumState{off,on}; Tv (intS=off,intMc= the): State (s), Maxchannel (MC), channel (2){}    voidOnOff () {state= (state==on)?Off:on;} voidChanup (); voidChandown (); voidSettings ()Const;Private:    intState ; intMaxchannel; intChannel;}; InlinevoidRemote::onoff (Tv &t) {T.onoff ();} InlinevoidRemote::chanup (Tv &t) {T.chanup ();} InlinevoidRemote::chandown (Tv &t) {T.chandown ();} InlinevoidRemote::set_chan (Tv &t,intc) {t.channel=C;}#endif

5. Common Friends
multiple classes require access to the same function, you can declare the function as a friend function in these classes . For example, if you have a counter function counter, two classes A and B call this function at the same time, you can declare the function counter as a friend function in classes A and B.

void counter () {    //... . }class  a{    int  counter ();} class b{    int  counter ();}

6. Note When using the friend class:
1) A friend relationship cannot be inherited.
2) friend relationship is one-way and not commutative. If Class B is a friend of Class A, class A is not necessarily a friend of Class B.
3) Friend relationship does not have transitivity. If Class B is a friend of Class A, Class C is a friend of B, and Class C is not necessarily a friend of Class A

Reference: "C + + primer.plus" pp.602-610

C + + friend detailed

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.