C++友元

友元包括友元函数和又元类

友元函数

友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。类授予它的友元特别的访问权。通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其它部分的拥有者的同意)。

普通友元函数

语法:
声明位置:公有私有均可,常写为公有
声明形式: friend + 普通函数声明
实现位置:可以在类外或类中
实现代码:在类外实现与普通函数相同(不加friend和类::)
一般特性:类似普通函数,可直接调用

如下面代码所示:

class Boy
{
    private:
       string name;
       int age;
    public:
       Boy(string sName, int iAge)
       {
           name = sName;
           age = iAge;
       }
       //内部声明需要加friend
       friend void makefriend(const Boy&b1,const Boy&b2);
};
void makefriend(const Boy&b1, const Boy&b2)
{
    cout<< "One boy's name is "<<b1.name<<", age is "<<b1.age<<endl;//访问对象中的private成员畅通无阻
    cout << "The other boy's name is "<<b2.name<<", age is "<<b2.age<<endl;
    cout << b1.name<<" and "<<b2.name<<" will make friends with each other."<<endl;
}

void testNormalFriend()
{
    Boy b1("Positive",23);
    Boy b2("persist",22);
    makefriend(b1, b2);//直接调用友元函数
}

其它类的成员函数作友元

语法:
声明位置:声明在公有域里
声明形式:friend + 成员函数的声明

如下面的代码

class Girl;
class Boy
{
    private:
       string name;
       int age;
    public:
       Boy(string sName, int iAge)
       {
           name = sName;
           age = iAge;
       }
       //成员函数
       void makeCouple(const Girl&);
};
class Girl
{ 
    private:
        string name;
        int age;
    public:
        Girl(string sName, int Iage)
        {
            name = sName;
            age = Iage;
        }
        //在Girl类里声明Boy的成员函数makeCouple为友元函数
        friend void Boy::makeCouple(const Girl&);
};


void Boy::makeCouple(const Girl&g)
{
    cout << "Boy's name is "<<name<<", age is "<<age<<endl;
    //友元函数访问Girl中的私有成员
    cout << "Girl's name is "<<g.name<<", age is "<<g.age<<endl;
    cout << "They become couple."<<endl;
}
void testMemberFuncFriend()
{
    Boy b("Positive",23);
    Girl g("Nice",21);
    b.makeCouple(g);
}

运算符重载友元函数

语法:
声明位置:公私有域均可
声明形式:friend 类名 函数名(参数),参数中必须有一个是对应类名的类型的
如下面的代码:

class Boy
{
    private:
       string name;
       int age;
    public:
       Boy(string sName, int iAge)
       {
           name = sName;
           age = iAge;
       }
       //友元函数重载运算符
       friend Boy operator ++(Boy&);
       void showMe()
       {
           cout<< "Boy's name is "<<name<<", age is "<<age<<endl;
       }
};

//运算符重载
Boy operator++(Boy &b)
{
    b.age++;
    return b;
}
void testOperatorFriend()
{
    Boy b("Positive",26);
    b.showMe();
    ++b;
    b.showMe();
}

友元类

声明A是B的友元类后,A中的成员函数就是B的友元函数了
语法:
声明位置:私有区域或公有区域均可
声明形式:friend + class + 类名
调用:作为友元类的成员函数来调用

如下面的代码:

class Girl;
class Boy
{
    private:
       string name;
       int age;
    public:
       Boy(string sName, int iAge)
       {
           name = sName;
           age = iAge;
       }
       void makeCouple(const Girl&);//Boy类的所有成员函数都可以作为Girl的友元来访问Girl
};
class Girl
{ 
    private:
        string name;
        int age;
        friend class Boy;//声明Boy为Girl的友元类
    public:
        Girl(string sName, int Iage)
        {
            name = sName;
            age = Iage;
        }
};
//makeCouple既是Boy的成员函数,也是Girl的友元函数
void Boy::makeCouple(const Girl&g)
{
    cout << "Boy's name is "<<name<<", age is "<<age<<endl;
    cout << "Girl's name is "<<g.name<<", age is "<<g.age<<endl;//可访问Girl的私有成员,正常情况只有Girl的成员函数才可以访问其私有成员
    cout << "They become couple."<<endl;
}
void testClassFriend()
{
    Boy b("Positive",23);
    Girl g("Nice",21);
    b.makeCouple(g);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值