友元函数和友元类

本文是在http://www.cppblog.com/twzheng/articles/21020.html的基础上,增加了两个示例代码。

 

采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口。但是,有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类的数据成员,这时可以将这些函数定义为该函数的友元函数。除了友元函数外,还有友元类,两者统称为友元。友元的作用是提高了程序的运行效率(即减少了类型检查和安全性检查等都需要时间开销),但它破坏了类的封装性和隐藏性,使得非成员函数可以访问类的私有成员。

 

一、友元函数

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

 

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

 

友元函数的声明可以放在类的私有部分,也可以放在公有部分,它们是没有区别的,都说明是该类的一个友元函数。

一个函数可以是多个类的友元函数,只需要在各个类中分别声明。

友元函数的调用与一般函数的调用方式和原理一致。

 

友元函数例子:

#include <iostream>

#include <math.h>

using namespace std;

 

class APoint

{

private:

         double x;

         double y;

public:

         APoint(double x, double y);

         void getCoordinates();

         friend double getDistance(const APoint&, const APoint&);

};

 

APoint::APoint(double x, double y)

{

         this->x = x;

         this->y = y;

}

 

void APoint::getCoordinates()

{

         cout << "(" << x << ", " << y << ")" << endl;

}

 

double getDistance(const APoint& a, const APoint& b)

{

         return sqrt(pow((a.x - b.x), 2.0) + pow((a.y - b.y), 2.0));

}

 

int main()

{

         APoint p1(3, 4);

         APoint p2(11, 12);

 

         p1.getCoordinates();

         p2.getCoordinates();

 

         double d = getDistance(p1, p2);

         cout << "The distance between p1 and p2 is: " << d << endl;

 

         return 0;

}

 

二、友元类

友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。      

 

当希望一个类可以存取另一个类的私有成员时,可以将该类声明为另一类的友元类。定义友元类的语句格式如下:

      friend class 类名;

其中:friendclass是关键字,类名必须是程序中的一个已定义过的类。

 

例如,以下语句说明类B是类A的友元类:

      class A

      {

             …

      public:

             friend class B;

             …

      };

经过以上说明后,类B的所有成员函数都是类A的友元函数,能存取类A的私有成员和保护成员。

 

使用友元类时注意:

(1)   友元关系不能被继承。

(2)   友元关系是单向的,不具有交换性。若类B是类A的友元,类A不一定是类B的友元,要看在类中

是否有相应的声明。

(3)       友元关系不具有传递性。若类B是类A的友元,类CB的友元,类C不一定是类A的友元,同样

要看类中是否有相应的申明

 

友元类例子:

// ------------------------------------------

// example: 友元类

#include <iostream>

#include <math.h>

using namespace std;

 

// class AFriendAPoint;      没有必要在此预先声明AFriendPoint

 

class APoint

{

private:

         double x;

         double y;

public:

         APoint(double x, double y)

         {

                   this->x = x;

                  this->y = y;

         }

         friend class AFriendPoint;

};

 

class AFriendPoint

{

public:

         void getCoordinates(const APoint&);

         double getDistance(const APoint&, const APoint&);

};

 

void AFriendPoint::getCoordinates(const APoint& p)

{

         cout << "This coordinates of this point is: (" << p.x << ", " << p.y << ")" << endl;

}

 

double AFriendPoint::getDistance(const APoint& a, const APoint& b)

{

         return sqrt(pow((a.x - b.x), 2.0) + pow((a.y - b.y), 2.0));

}

 

int main()

{

         APoint p1(3, 4);

         APoint p2(4, 5);

         AFriendPoint* afp = new AFriendPoint;

         afp->getCoordinates(p1);

         afp->getCoordinates(p2);

        

         double the_distance = afp->getDistance(p1, p2);

         cout << "The distance between those 2 points is: " << the_distance << endl;

 

         delete afp;

 

         return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值