本文试图解释c++ primer Screen 和 Window_Mgr的例子,为什么将两个类放在两个文件中无法编译?

将两个类写在同一个文件中,通过三个例子解释问题:

第一种写法问题:

编译到Screen时,由于Screen类使用到Window_Mgr的成员函数,虽然前面给出了Window_Mgr的声明,但此时还清楚Window_Mgr的完整定义,所以编译出错。

class Window_Mgr

class Screen

{

public:

friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s);

private:

int height;

int width;

}

class Window_Mgr

{

public:

typedef std::string::size_type index;

Window_Mgr& Window_Mgr::relocate(index r, index c, Screen& s)

{

s.height += r;

s.width += c;

return *this;

}

}

第二种写法问题在于:

编译到relocate时,由于Screen& s的实现使用到Screen的成员变量,虽然前面给出了Screen的声明,但此时还清楚Screen的完整定义,所以编译出错。

class Screen;

class Window_Mgr

{

public:

typedef std::string::size_type index;

Window_Mgr& Window_Mgr::relocate(index r, index c, Screen& s)

{

s.height += r;

s.width += c;

return *this;

}

}

class Screen

{

public:

friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s);

private:

int height;

int width;

}

第三种写法:

将Window_Mgr::relocate的实现移动到最后,由于编译类Window_Mgr时,并不需要Screen&s 的实现细节,问题得到解决

class Screen;

class Window_Mgr

{

public:

typedef std::string::size_type index;

Window_Mgr& Window_Mgr::relocate(index r, index c, Screen& s);

}

class Screen

{

public:

friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s);

private:

int height;

int width;

}

Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s)

{

s.height += r;

s.width += c;

return *this;

}

可见,这两个类如果编译成功需要严格的交替顺序

这也就解释了为什么放在两个文件中无法编译。

附录:

一开始的实现的不能编译的两个文件

实现分别如下:Window_Mgr.h

#ifndef WINDOW_MGR //为了避免两个文件嵌套

#define WINDOW_MGR

#include <string>

#include <Screen.h>

class Window_Mgr

{

public:

typedef std::string::size_type index;

Window_Mgr& Window_Mgr::relocate(index r, index c, Screen& s)

{

s.height += r;

s.width += c;

return *this;

}

}

#endif

Screen.h

#ifndef SCREEN

#define SCREEN

#include "Window_Mgr.h"

class Screen

{

public:

friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s);

private:

int height;

int width;

}

#endif

c++ primer,友元函数上的一个例子(By Sybase)相关推荐

  1. linux运行qt桌面生成pro怎么写,把桌面qt代码编译运行到qpe上的一个例子

    qt-x11版本是2.3.2 qt core版本是2.3.2 qtopia版本是2.2.x qt-x11版本安装目录下的tutorial目录下有一些例子.我把其中的t7目录下的程序(这是一个lcd数值 ...

  2. c++关联容器的成员函数find的一个例子

    先来一个灰色难懂的部分镇楼. #include <string> #include <iostream> #include <list> #include < ...

  3. SpringBoot应用和PostgreSQL数据库部署到Kubernetes上的一个例子

    创建一个名为ads-app-service的服务: 上述Service的yaml文件里每个字段,在Kubernetes的API文档里有详细说明. https://kubernetes.io/docs/ ...

  4. C++的友元函数,友元类及#pragma once的作用

    把代码都放最后吧 友元函数的作用: 可以访问该类的私有属性 但在类的外面定义的时候没有"类::" 所以该友元函数不属于该类 友元函数的意义: C++友元函数_Bussy的博客-CS ...

  5. C++运算符重载函数作为友元函数

    运算符重载函数作为友元函数 在前面的程序例子中对运算符"+"进行了重载,使之能用于两个字符串的相加.在该例中运算符重载函数 operator +(); 作为 my_string类中 ...

  6. 初入c++(三)this指针,友元函数,友元类

    1.c++中的this指针 指向当前对象,通过它可以访问当前对象的所有成员.当前对象就是正在使用的对象: 在类的内部使用,可以访问所有的成员,public,private,protect this只能 ...

  7. java有没有友元函数_c++中友元函数理解与使用

    在学习c++这一块,关于友元函数和友元类,感觉还是不好理解,但是井下心来,理解,需要把我一下几点. 首先讲友元函数. (1)友元函数: 1)C++中引入友元函数,是为在该类中提供一个对外(除了他自己意 ...

  8. C++之友元函数和友元类讲解(一百一二十七)

    目录 0.C++类中成员变量和成员函数的访问权限 <0>.C++ 类中可以有 public.protected.private 三种属性的成员. <1>.外部类可以通过实例化对 ...

  9. 【C++】模板类的友元函数

    模板类友元函数 模板类的友元函数 参考:https://blog.csdn.net/dreamer_lhs/article/details/53580088 区分:友元是否为函数模板 非模板友元 约束 ...

最新文章

  1. 社区医学的研究方法:调查、流行病学研究、方案评估、临床试验Research Methods in Community Medicine: Surveys, Epidemiological Resear
  2. php文件解锁,php文件锁怎么用
  3. 插件化、热补丁中绕不开的Proguard的坑
  4. Amazon S3数据一致性模型
  5. python设计模式18-备忘录模式
  6. 均分纸牌模型之mxj分礼物
  7. db2 删除索引_程序员必须了解的知识点——你搞懂mysql索引机制了吗?
  8. DRY(Don't Repeat Yourself )原则
  9. Vue项目上线的基本流程
  10. 胚胎干细胞研究成果集锦,目录大合集
  11. 快消品行业B2B电商平台解决方案
  12. 解决Chrome或Microsoft Edge浏览器打开时自动跳转到hao123
  13. leetcode1438
  14. 一秒批量修改文件扩展名(后缀名)
  15. Monitor(管程)是什么意思?Java中Monitor(管程)的介绍
  16. 99的测试人还不会用nose进行自动化测试
  17. 送5本新出版的《剑指offer》
  18. LWIP (chapter 2.01) pbuf数据包缓存
  19. 智能手机低价成潮,vivo为何执念高端?
  20. 安徽省计算机学校排名,2018“中国最好学科排名”公布 安徽这14所高校上榜

热门文章

  1. 2019年春节记忆之尹山湖边赏梅
  2. 空车上路,Waymo拿下加州首个“真”无人驾驶许可证
  3. SAP MM 按采购订单查询付款信息的报表?
  4. Tensorflow— saver_save
  5. 今日《科学》封面:纳米级清晰度看大脑是怎样一种体验?
  6. 一图分析华为最新AI生态与未来趋势
  7. GAN还有这种操作!谷歌大脑和X实验室利用模拟条件和域适应提高机器抓取效率(附论文)
  8. 程序员都想,却不敢做的事?我来!
  9. 程序员毕业两年,如何在帝都购房上车?
  10. 遇到这四种面试官,接了 Offer 你可能会后悔