If only member function clear of WindowMgr is a friend of Screen, there are some points need to note. Sequence is important.

  • First, define the WindowMgr class in WindowMgr.h, which declares, but cannot define clear function. Screen must be declared before clear can use the members of Screen.
  • Next, define class Screen in Screen. h, including a friend declaration for clear.
  • Finally, define clear in WindowMgr.cpp, which can now refer to the members in Screen.

    When trying to separate class WindowMgr and class Screen in different files for a better decoupling, placed "#include 'Screen.h" statement in front of class WindowMgr`s declare will cause that the friend declaration for clear in Screen.h can't find the corresponding function, because when declaring class Screen surrounding scope can't see the declaration of WindowMgr.

    So the resolution is placing "#include Screen.h" statement in bottom of class WindowMgr s declaration and forward declaration class Screen for WindowMgr.h, or declare a friend for class WindowMgr.

如果 WindowMgr 的成员函数 clearScreen 的友元函数,则需要注意一些要点。顺序很重要。

  • 首先,在WindowMgr.h中定义 WindowMgr 类,只是进行类声明,但不能定义clear函数。必须在clear之前声明 Screen 才能使用 Screen 的成员。
  • 接下来,在Screen中定义class Screen . h,包括 clear 的朋友声明。
  • 最后,在WindowMgr.cpp中定义 clear,它现在可以引用 Screen 中的成员。

    当试图在不同的文件中分离类 WindowMgr 和类 Screen 以获得更好的解耦效果时,在类 WindowMgr 的声明前面放置 "#include 'Screen.h" 语句将导致在Screen.h中声明的友元不能找到相应的函数,因为在声明类 Screen 时,周围的作用域无法看到 WindowMgr 的声明。

    因此,解决方案是将“#include Screen.h”语句放在类 WindowMgr 的声明的底部,并且在WindowMgr.h中前向声明类 Screen 。或者直接为类 WindowMgr 声明一个友元类 Screen

About The Order of The Declarations And Definition When Making a Member Function a Friend.关于使类成员成为另一个类友元函数的声明顺序和定义。的更多相关文章

  1. Method Resolution Order – Python类的方法解析顺序

    在支持多重继承的编程语言中,查找方法具体来自那个类时的基类搜索顺序通常被称为方法解析顺序(Method Resolution Order),简称MRO.(Python中查找其它属性也遵循同一规则.)对 ...

  2. Lua语言中文手册 转载自网络

    Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...

  3. 精心收集java基础106条

    Java基础 1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 一个Java源文件中可以定义多个类,但最多只能定义一个public的类,并且public ...

  4. Sqlite3中存储类型和数据类型结合文档解析。

    sqlite3是个很小的数据库,运行在手机,机顶盒上....那它就不可能像musql,sqlserver那么规范,有很多的数据类型,之前我也以为它定义了很多数据类型,其实不是他就5个存储类,那么多数据 ...

  5. Java编程思想读书笔记

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. (C++) 基本面试题(整理)

    1.new.delete.malloc.free关系 new/delete是C++的运算符.new 调用构造函数用于动态申请内存,delete调用对象的析构函数,用于释放内存. malloc与free ...

  7. [转]使用CSS3 Grid布局实现内容优先

    使用CSS3 Grid布局实现内容优先  http://www.w3cplus.com/css3/css3-grid-layout-module.html 本文由大漠根据Rachel Andrew的& ...

  8. C++转义字符 & keyword

    转义字符: 换行符 \n   水平制表符\t 纵向制表符 \v 退格符 \b 回车符 \r   进纸符 \f 报警(响铃)符 \a 反斜线 \\ 疑问号 \? 单引號 \' 双引號 \"   ...

  9. Java中调用参数是数组的存储过程

    Java中调用参数是数组的存储过程 1. 存储过程以及类型定义如下: --The array in oracle CREATE OR REPLACE TYPE idArray AS TABLE OF ...

随机推荐

  1. node配置环境变量

    package.json "scripts": { "start_test": "cross-env BUILD_ENV=dev nuxt start ...

  2. CF1152E Neko and Flashback--欧拉路径

    RemoteJudge 第一次见到欧拉路径的题 注意到\(b\)和\(c\)的构造方法很特殊,即对于一个位置(经过\(p\)作用后)\(i\),若两个数分别为\(b_i\)和\(c_i\),那么在\( ...

  3. JMeter二次开发环境配置

    本文主要介绍如何在Eclipse中配置JMeter开发环境. 一.下载JMeter源码 1.在JMeter官网下载二进制包和源码包: 解压备用: 二进制解压后文件夹名称为“jmeter_release ...

  4. [C#] .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  5. 小程序 input type number 键盘没有小数点

    在此备注一下: <input type="digit"> number:数字键盘(无小数点)idcard:数字键盘(无小数点.有个 X 键)digit:数字键盘(有小数 ...

  6. python2和python3的区别

    python2和python3的区别 参考链接:http://www.runoob.com/python/python-2x-3x.html 1.源码上的区别 python2 python3 源码不规 ...

  7. js循环内0.5s停止

    var time_begin = new Date().getTime(); while(true){ if(new Date().getTime()-time_begin > 500) bre ...

  8. 集智人工智能学习笔记Python#0

    1,学习基本Python语句规范: print('Hello world') print() 为函数 ‘Hello world’为字符串 2,表达式和语句的区别: 表达式有结果,运算就是表达式的一种: ...

  9. ie浏览器部分图片不显示

    前言 前几天做项目时,发现一个奇怪的现象,从后台获取的图片,在IE浏览器端,有一部分不会显示,仔细研究发现是图片本来是.jpg格式,后台传过来的图片后缀已经被改成了.png格式或者其它格式导致IE浏览 ...

  10. 前端使用pdf.js预览pdf文件

    现在的浏览器基本都支持直接把pdf文件拖到浏览器就可以打开了,不用安装额外的插件.但是不同的浏览器显示的页面不一样.如果我们想在网页上统一预览pdf怎样实现呢? Mozilla开源了一个插件pdf.j ...