C++友元

来源:互联网 发布:java中的多态 编辑:程序博客网 时间:2024/06/05 10:33

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

友元函数

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

普通友元函数

语法:
声明位置:公有私有均可,常写为公有
声明形式: 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
原创粉丝点击