Step-by-step learning of C + + (class) friend functions

Source: Internet
Author: User
Tags function prototype

description of use of friend function 1) The Friend function must be stated in the description of the class, stating that the friend function is preceded by a keyword friend, followed by a function prototype of the friend function, and that the description of the friends can appear anywhere in the class, including the private and public parts; 2) Note that the friend function is not a member function of the class, so the implementation of the friend function is the same as the normal function, without "::" to indicate which class belongs to, only the member function uses the "::" scope symbol; 3) The friend function cannot access the members of the class directly, only the object members, 4) The friend function can access the private member of the object, but the normal function is not; 5) When you adjust the function, you need to indicate the object to be accessed in the actual parameters . 6) A friend relationship between a class and a class cannot be inherited. 7) A member function of a class can also be a friend of another class, but this class must be defined first
<span style= "FONT-SIZE:12PX;" >//FriendForClass.cpp: Defines the entry point of the console application. #include "stdafx.h" class Myclassy; Declare class myclassz in advance; Declare in advance class MYCLASSX {friend class myclassz;public:void print (), void SetX (Myclassy &s,int dis), friend void distance ( MYCLASSX &a,myclassx &b);/* friend function */private:int x;int y;static int z;}; int myclassx::z = 0;//Be sure to remember that assignment class myclassy{public://MYCLASSX can access Myclassy members private or protect data friend void Myclassx::setx ( Myclassy &s,int dis);/* friend function */private:int x;int y;}; Class myclassz{public://Myclassz can access myclassx all members void Displayfor ();/* For access to MYCLASSZ members */private:int Z1;int Z2; MYCLASSX x;//need to define an object here};/*********************************************************/// MYCLASSZ can access MYCLASSX must pass the object of the class, indirectly access member void MYCLASSZ::d isplayfor () {x.x = 10; X.z = 10; Myclassx::z = 10;} void Myclassx::setx (Myclassy&s,int dis) {s.x = dis;} /******************************************************///cannot directly access members of a class, must pass through the class's object, indirectly access member void distance (MYCLASSX & A,MYCLASSX &b) {int x1 = A.x;int Y1 = b.y;printf ("%d%d", x1,y1);} The/*********************************///member function can directly access the members of the class void MYCLASSX::p rint () {printf ("%d%d", x, y);} int _tmain (int argc, _tchar* argv[]) {return 0;} </span>


Step-by-step learning of C + + (class) friend functions

Related Article

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.