C + + Learning notes-friend

Source: Internet
Author: User

C + + Controls access to the private part of the class object, and the private or protected members of the class cannot be accessed directly outside. Generally, public-class methods provide a unique access path. Sometimes this restriction is too restrictive to be appropriate for a particular programming problem. So C + + provides a friend of this form, by making a function or class A friend of Class A, you can give the function or class A member function with the same access rights.

There are 3 types of friends:

(1) Friend function

(2) Friend class

(3) Friend member function

1, friend function

Put its prototype in the class declaration and add the Friend keyword

Such as:

Class time{public://friend function friend time operator* (double N, const time & T);//overloaded << operator friend Std::ostream & Oper ator<< (Std::ostream & OS, const time & T);p rivate:int m_hours;int m_minutes;};

Attention:

(1) operator* () is declared in a class declaration, but it is not a class member function, so when defining a function, you cannot use the time:: qualifier.

(2) operator* () is not a member function, but has the same access rights as member functions.

In summary, the friend function of a class is a non-member function, and its access rights are the same as member functions.

2, Friend class

You can use a class as a friend, and all methods of a friend class can access the private and protected members of the original class.

The following friend class Remote; Declare remote as a friend of the TV class:

#ifndef tv_h_#define tv_h_//TV class Tv{public:friend class remote;//declares that the Remote class is a friend of the TV class, All methods of the remote class can access the private and protected members of the TV enum{off,on};enum{minval,maxval = 20}; Tv (int s = off,int MC = +), ~tv (void), void On_off () {state = (state = = on? off:on); BOOL Is_on () const{return state = = on;}; BOOL Vol_up (); bool Vol_down (); void channel_up (); void Channel_down (); void Show_settings () const;private:int State;int Volume;int Maxchannel;int channel;};/ /Remote Control class Remote{public:bool VOL_UP (TV & TV) {return tv.vol_up ();}; BOOL Vol_down (TV & TV) {return Tv.vol_down ();}; void On_off (TV & TV) {Tv.on_off ();}; void CHANNEL_UP (TV & TV) {tv.channel_up ();}; void Channel_down (TV & TV) {Tv.channel_down ();}; void Set_channel (TV & TV, int ch) {Tv.channel = ch;};/ /friend can access the private member of the original class}; #endif

a friend declaration can be in the public, private, or protected section, where it does not matter.

3, friend member function

In the remote class, only the Set_channel (TV & TV, int ch) method accesses the private members of the TV class directly, so you can choose to make this method a friend of the class without having to make the entire remote class A friend. However, this must be done carefully by arranging the order of declarations and definitions.

To make Remote::set_channel () a friend of the TV class, the non-method is to declare it as a friend in the TV class:

Class tv{   friend void Remote::set_channel (TV & TV, int ch);};

C + + Learning notes-friend

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.