C++ -- 友元函数总结

本文深入探讨了C++中的友元机制,包括非成员函数作为友元、类的成员函数作为友元以及类作为友元的三种形式,并通过实例详细讲解了每种友元的使用方法。

 

引言

C++中的友元机制允许类的非公有成员被一个类或者函数访问,友元按类型分为三种:1、非类成员函数作为友元;2、类的成员函数作为友元;3、类作为友元。友元的声明默认为extern,意思是友元类或者友元函数的作用域已经扩展到了包含该类定义的作用域。

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

       接下来将用最简单的方式进行测试说明。

一、非成员函数友元

友元函数是可以直接访问类的私有成员的非成员函数

#ifndef ADD_H
#define ADD_H
class add
{
    public:
        friend void caculate();//定义一个友元函数
        add();
        virtual ~add();

    protected:

    private:
};
void caculate();//申明该友元函数
#endif // ADD_H
#include "add.h"
#include <iostream>
using namespace std;
add::add()
{
    cout << "add function" << endl;
}
void caculate()
{
    cout << "friend" << endl;
}
add::~add()
{
    //dtor
}
#ifndef APP_H
#define APP_H
class app
{
    public:
        app();
        void test_app();
        virtual ~app();

    protected:

    private:
};

#endif // APP_H
#include "app.h"
#include<iostream>
using namespace std;
app::app()
{
    cout << "app function" << endl;
}
void app::test_app()
{
    cout << "test_app()" << endl;
}
app::~app()
{
    //dtor
}
#include <iostream>
#include "add.h"
#include "app.h"

using namespace std;

int main()
{
    app test_app;
    test_app.test_app();  //类中成员函数的调用方式
    caculate();           //非成员友元函数的调用
    cout << "Hello world!" << endl;
    return 0;
}

二、类成员函数友元

使类A的一个成员函数成为类B的友元,具体而言:在类A的这个成员函数中,借助成员函数友元,可以直接访问类B的私有变量

注意调用关系:先定义包含友元函数的类,再定义原始类,这个顺序不能乱。即先定义 指定为友元成员函数a的类A,再定义类B,类B中包含了友元函数a,使得a可以调用类B中的私有变量。

先定义成员友元函数的类:

#ifndef ADD_H
#define ADD_H
class add
{
  public:
     void caculate();//将其定义为成员函数友元
     add();
     virtual ~add();

  protected:
  
  private:
};
#endif // ADD_H

再定义包含该友元函数的类:

#ifndef APP_H
#define APP_H
#include "add.h"
class app
{
  public:
    app();
    void test_app();
    virtual ~app();
    friend void add::caculate();
  protected:
  private:
      int prot_num=666;
};

#endif // APP_H


 cpp文件定义:
 

#include "add.h"
#include <iostream>
#include "app.h"
using namespace std;
add::add()
{
    cout << "add function" << endl;
}
void add::caculate()
{
	app app_test;
    cout << "app private num is:" << app_test.prot_num << endl;
}
add::~add()
{
    //dtor
}
#include "app.h"
#include<iostream>
using namespace std;
app::app()
{
    cout << "app function" << endl;
}
void app::test_app()
{
    cout << "test_app()" << endl;
}
app::~app()
{
    //dtor
}

主函数:

#include <iostream>
#include "add.h"
#include "app.h"

using namespace std;

int main()
{
        add test_add;
        test_add.caculate();           //add的友元成员函数调用app中的私有变量
        cout << "Hello world!" << endl;
        return 0;
}

三、友元类

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

注意:类作为友元需要注意的是友元类A和原始类B之间的相互依赖关系,如果在友元类A中定义的函数使用到了原始类B的私有变量,那么就需要在友元类A定义的文件中包含原始类B定义的头文件。
但是在原始类B的定义中(包含友元类A声明的那个类),就不需要包含友元类A的头文件,也不需要在类定义前去声明友元类A,因为友元类的声明自身就是一种声明(它指明可以在类外找到友元类)

定义友元类A(app)

#ifndef APP_H
#define APP_H
#include <string>
using namespace std;
class app
{
    public:
        app();
        void test_app();
        virtual ~app();

    protected:
        string add_test="this is app class";
    private:
};

#endif // APP_H
#include "app.h"
#include<iostream>
#include "add.h"  //由于要使用add,所以要定义其头文件

app::app()
{
    cout << "app function" << endl;
}
void app::test_app()
{
    add test_add;
    cout << test_add.add_test << endl;
}
app::~app()
{
    //dtor
}

定义原始类B(add)

#ifndef ADD_H
#define ADD_H
#include <string>
using namespace std;
class add
{
    public:
        friend class app;//不用定义app的头文件
        add();
        virtual ~add();

    protected:
        string add_test="this is add class";

    private:
};
#endif // ADD_H
#include "add.h"
#include <iostream>

add::add()
{
    cout << "add function" << endl;
}

add::~add()
{
    //dtor
}

主函数:

#include <iostream>
#include "add.h"
#include "app.h"

using namespace std;

int main()
{
    app test_app;
    test_app.test_app();  //友元类app调用add中受保护的成员变量
    cout << "Hello world!" << endl;
    return 0;
}

至此,友元的三种使用方法介绍完毕。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值