Friends of C ++ specifications

Source: Internet
Author: User
Document directory
  • Friend Functions
  • Friend classes
Friend Functions

In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affectFriends.

Friends are functions or classes declared withfriendKeyword.

If we want to declare an external function as Friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within
Class, and preceding it with the keywordFriend:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// friend functions#include <iostream>using namespace std;class CRectangle {    int width, height;  public:    void set_values (int, int);    int area () {return (width * height);}    friend CRectangle duplicate (CRectangle);};void CRectangle::set_values (int a, int b) {  width = a;  height = b;}CRectangle duplicate (CRectangle rectparam){  CRectangle rectres;  rectres.width = rectparam.width*2;  rectres.height = rectparam.height*2;  return (rectres);}int main () {  CRectangle rect, rectb;  rect.set_values (2,3);  rectb = duplicate (rect);  cout << rectb.area();  return 0;}
24 

TheDuplicateFunction is a friendCrectangle. From within that function we have been able to access the membersWidthAnd
HeightOf different objects of TypeCrectangle, Which are private members. Notice that neither in the declarationDuplicate ()Nor in its later use in
Main ()Have we consideredDuplicateA member of classCrectangle. It isn' t! It simply has access to its private and protected members without being a member.

The friend functions can serve, for example, to conducting operations between two different classes. generally, the use of friend functions is out of an object-oriented programming methodology, so whenever possible it is better to use members of the same class
To perform operations with them. Such as in the previous example, it wowould have been shorter to integrateDuplicate ()Within the class
Crectangle.

Friend classes

Just as we have the possibility to define a friend function, we can also define a class as friend of another one, granting that first class access to the protected and Private Members of the second one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// friend class#include <iostream>using namespace std;class CSquare;class CRectangle {    int width, height;  public:    int area ()      {return (width * height);}    void convert (CSquare a);};class CSquare {  private:    int side;  public:    void set_side (int a)      {side=a;}    friend class CRectangle;};void CRectangle::convert (CSquare a) {  width = a.side;  height = a.side;}  int main () {  CSquare sqr;  CRectangle rect;  sqr.set_side(4);  rect.convert(sqr);  cout << rect.area();  return 0;}
16

In this example, we have declaredCrectangleAs a friendCsquareSo thatCrectangleMember functions cocould have access to the protected and private membersCsquare, More concretely
Csquare: Side, Which describes the side width of the square.

You may also see something new at the beginning of the program: An empty declaration of classCsquare. This is necessary because within the Declaration
CrectangleWe refer to csquare (as a parameter inConvert ()). The definition
CsquareIs already ded later, so if we did not include a previous empty declarationCsquareThis class wocould not be visible from within the definition
Crectangle.

Consider that friendships are not corresponded if we do not explicitly specify so. In our example,CrectangleIs considered as a friend class
Csquare,CrectangleDoes not considerCsquareTo be a friend, so
CrectangleCan access the protected and private membersCsquareBut not the reverse way. Of course, we cocould have declared also
CsquareAs friendCrectangleIf we wanted.

Another property of friendships is that they areNot transitive: The friend of a friend is not considered to be a friend unless explicitly specified.

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.