IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> C++友元成员函数 -> 正文阅读

[开发工具]C++友元成员函数

友元成员函数

?????? 在前面的例子中,我们定义了一个普通的函数,然后,在定义一个类的时候,把这个函数声明为该类的“朋友”,可以让该函数访问类中的成员变量。

?????? 同样,我们也在定义一个类的时候,把另一个类的成员函数声明为该类的朋友,让它可以访问自己的成员变量。

?????? 所以,firent 函数不仅可以定义一个普通函数(非成员函数),而且,可以是另一个类中的成员函数。下面举例介绍友元成员函数的使用。

?????? 在这个例子中,除了介绍有关友元成员函数的简单应用外,还将用到类的“提前引用声明”。就是说,一个类在使用之前,没有被定义,而是放在后面定义,那么,我们可以在使用之前先声明它。有如下的类定义:

class student;//提前声明student;

class my_print//定义 my_print ;

{

public:

??? void print(student& s); //声明函数;

};

class student{

private: //定义私有类型成员变量

??? char name[32]; //姓名

??? char addr[32]; //家庭地址

??? long long number; //电话号码

public: //以下部分是公有部分

??? student(char* pn, char* pa, long long n)

??? {

??????? strcpy(name, pn);

??????? strcpy(addr, pa);

??????? number = n;

??? }

??? friend void my_print::print(student& s);//声明友元函数;

};

?????? 可以看到,定义了my_print类和student类。其中,在my_print类中引用student类。而且,student类在最后面定义。

?????? 那么,就在my_print类之前,编写如下的语句:

class student;//提前声明student;

?????? 表示提前声明student类,那么,就可以在my_print类中引用student类。完整的测试代码如下:

?

?????? 程序编译结果如下:

wkf@ubuntu:~/c++$ g++ test.cpp -o exe

test.cpp: In member function ‘void my_print::print(student&)’:

test.cpp:14: error: invalid use of incomplete type ‘struct student’

test.cpp:6: error: forward declaration of ‘struct student’

test.cpp:15: error: invalid use of incomplete type ‘struct student’

test.cpp:6: error: forward declaration of ‘struct student’

test.cpp:16: error: invalid use of incomplete type ‘struct student’

test.cpp:6: error: forward declaration of ‘struct student’

?????? 可以看到,在my_print::print()函数中,引用student类型的数据异常。为什么会出现这个问题?

?????? 因为,在my_print::print()函数中,引用student类的时候,这个student类还没有定义。我们只是在my_print类之前提前声明了student类。并没有定义student类,所以,编译器在my_print::print()函数中,无法访问s.name, s.addr等成员变量。

?????? 所以,我们可以把my_print::print()函数放在student类定义的后面。表示定义了student类之后,再定义my_print::print()函数,此时,编译器才知道s.name, s.addr成员变量是合法的定义。完整的测试代码如下:

?

?????? 程序运行结果如下:

?

?????? 可以看到,my_print::print()函数能够正确引用了student类中定义的private私有成员。这是因为,在student类中,声明my_print类的print()函数是友元类型,如下:

class student{

...

??? friend void my_print::print(student& s);//声明友元函数;

};

?????? 所以,student类中的私有成员,就可以在my_print类的print()函数中被访问。

  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2022-05-26 15:22:49  更:2022-05-26 15:23:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 16:01:08-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码