首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >友元函数和友元类

友元函数和友元类

作者头像
chenjx85
发布2019-05-25 17:15:54
1K0
发布2019-05-25 17:15:54
举报

友元提供了不同类的成员函数之间、类的成员函数与一般函数之间进行数据共享的机制。通过友元,一个不同函数或另一个类中的成员函数可以访问类中的私有成员和保护成员。c++中的友元为封装隐藏这堵不透明的墙开了一个小孔,外界可以通过这个小孔窥视内部的秘密。

友元的正确使用能提高程序的运行效率,但同时也破坏了类的封装性和数据的隐藏性,导致程序可维护性变差。

友元函数

友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend,其格式如下:

friend 类型 函数名(形式参数);

<!-- .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } --><!-- .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } -->

友元函数的声明可以放在类的私有部分,也可以放在公有部分,它们是没有区别的,都说明是该类的一个友元函数。 一个函数可以是多个类的友元函数,只需要在各个类中分别声明。 友元函数的调用与一般函数的调用方式和原理一致。

友元类 友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。 当希望一个类可以存取另一个类的私有成员时,可以将该类声明为另一类的友元类。定义友元类的语句格式如下: friend class 类名; 其中:friend和class是关键字,类名必须是程序中的一个已定义过的类。

   1:  #include <iostream>
   2:  using namespace std;
   3:   
   4:  class Radius
   5:  {
   6:      friend class Circle;                         //声明Circle为Radius的友元类
   7:      friend void Show_r(Radius &n);     //声明Show_r为友元函数
   8:  public:
   9:      Radius(int x)
  10:      {
  11:          r = x;
  12:      }
  13:      ~Radius()
  14:      {
  15:      }
  16:   
  17:  private:
  18:      int r;
  19:  };
  20:   
  21:  void Show_r(Radius &n)
  22:  {
  23:      cout<<"圆的半径为: "<<n.r<<endl;    //调用Radius对象的私有成员变量r
  24:  }
  25:   
  26:  class Circle
  27:  {
  28:  public:
  29:      Circle()    {}
  30:      ~Circle(){}
  31:      double area(Radius a)
  32:      {
  33:          s = a.r * a.r * 3.1415926;             //调用Radius对象的私有成员变量r
  34:          return s;
  35:      }
  36:  private:
  37:      double s;
  38:  };
  39:   
  40:  int main(int argc, char *argv[])
  41:  {
  42:      Radius objRadius(9);
  43:      Circle objCircle;
  44:   
  45:      Show_r( objRadius );
  46:      cout<<"面积为:"<<objCircle.area(objRadius)<<endl;
  47:   
  48:      return 0;
  49:  }

<!-- .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } -->

被声明两个类的友元声明

如果我们决定一个函数必须被声明为两个类的友元则友元声明如下

   1:  class Window; // 只声明
   2:   
   3:  class Screen
   4:   
   5:  {
   6:   
   7:  friend bool is_equal( Screen &, Window & );
   8:   
   9:  // ...
  10:   
  11:  };
  12:   
  13:  class Window 
  14:   
  15:  {
  16:   
  17:  friend bool is_equal( Screen &, Window & );
  18:   
  19:  // ...
  20:   
  21:  };

<!-- .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } -->

作为一个类的函数又是另一个类的友元

如果我们决定该函数必须作为一个类的成员函数并又是另一个类的友元,则成员函数声明和友元声明如下:

   1:  class Window;
   2:   
   3:  class Screen
   4:   
   5:  {
   6:   
   7:  public:
   8:   
   9:  // copy 是类 Screen 的成员
  10:   
  11:  Screen& copy( Window & );
  12:   
  13:  // ...
  14:   
  15:  };
  16:   
  17:  class Window
  18:   
  19:  {
  20:   
  21:  // copy 是类 Window 的一个友元
  22:   
  23:  friend Screen& Screen::copy( Window & );
  24:   
  25:  // ...
  26:   
  27:  };

<!-- .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } -->

只有当一个类的定义已经被看到时它的成员函数才能被声明为另一个类的友元。这并不总是能够做到的。

例如如果Screen 类必须把Window 类的成员函数声明为友元,而Window类必须把Screen 类的成员函数声明为友元。该怎么办呢?在这种情况下可以把整个Window类声明为Screen 类的友元。

例如:

   1:  class Window;
   2:   
   3:  class Screen
   4:   
   5:  {
   6:   
   7:  friend class Window;
   8:   
   9:  // ...
  10:   
  11:  };

<!-- .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Screen 类的非公有成员现在可以被Window 的每个成员函数访问。

使用友元类时注意: (1) 友元关系不能被继承。 (2) 友元关系是单向的,不具有交换性。若类B是类A的友元,类A不一定是类B的友元,要看在类中是否有相应的声明。 (3) 友元关系不具有传递性。若类B是类A的友元,类C是B的友元,类C不一定是类A的友元,同样要看类中是否有相应的申明

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-11-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 被声明两个类的友元声明
  • 作为一个类的函数又是另一个类的友元
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档