• 友元函数和友元类


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

    一、友元函数

    友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字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的友元,类C是B的友元,类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;

    }

    Fight fight fight ! 你有你的奇迹 ! Fight fight fight ! Just to be yourself !
  • 相关阅读:
    我的2015技术学习流水账
    Java程序员的现代RPC指南
    我对读计算机软件专业硕士的几点看法
    Hadoop加速器GridGain
    BDD敏捷开发入门与实战
    openstack kilo部署-基础环境
    python学习之成员信息增删改查
    网站监控系统安装部署(zabbix,nagios)
    监控redis数据库应用状态:python,tornado实现
    python 远程执行命令、发布文件
  • 原文地址:https://www.cnblogs.com/sjlove/p/3169661.html
Copyright © 2020-2023  润新知