About The Order of The Declarations And Definition When Making a Member Function a Friend.关于使类成员成为另一个类友元函数的声明顺序和定义。
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 的成员函数 clear 是 Screen 的友元函数,则需要注意一些要点。顺序很重要。
- 首先,在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.关于使类成员成为另一个类友元函数的声明顺序和定义。的更多相关文章
- Method Resolution Order – Python类的方法解析顺序
在支持多重继承的编程语言中,查找方法具体来自那个类时的基类搜索顺序通常被称为方法解析顺序(Method Resolution Order),简称MRO.(Python中查找其它属性也遵循同一规则.)对 ...
- Lua语言中文手册 转载自网络
Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...
- 精心收集java基础106条
Java基础 1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 一个Java源文件中可以定义多个类,但最多只能定义一个public的类,并且public ...
- Sqlite3中存储类型和数据类型结合文档解析。
sqlite3是个很小的数据库,运行在手机,机顶盒上....那它就不可能像musql,sqlserver那么规范,有很多的数据类型,之前我也以为它定义了很多数据类型,其实不是他就5个存储类,那么多数据 ...
- Java编程思想读书笔记
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- (C++) 基本面试题(整理)
1.new.delete.malloc.free关系 new/delete是C++的运算符.new 调用构造函数用于动态申请内存,delete调用对象的析构函数,用于释放内存. malloc与free ...
- [转]使用CSS3 Grid布局实现内容优先
使用CSS3 Grid布局实现内容优先 http://www.w3cplus.com/css3/css3-grid-layout-module.html 本文由大漠根据Rachel Andrew的& ...
- C++转义字符 & keyword
转义字符: 换行符 \n 水平制表符\t 纵向制表符 \v 退格符 \b 回车符 \r 进纸符 \f 报警(响铃)符 \a 反斜线 \\ 疑问号 \? 单引號 \' 双引號 \" ...
- Java中调用参数是数组的存储过程
Java中调用参数是数组的存储过程 1. 存储过程以及类型定义如下: --The array in oracle CREATE OR REPLACE TYPE idArray AS TABLE OF ...
随机推荐
- Wireshark显示结果过滤基本语法
按IP地址过滤:1.仅显示源地址为192.168.1.95的项目: ip.src eq 192.168.1.95 2.仅显示目的地址为192.168.1.95的项目: ip.dst eq 192.16 ...
- Promise的源码实现(完美符合Promise/A+规范)
Promise是前端面试中的高频问题,我作为面试官的时候,问Promise的概率超过90%,据我所知,大多数公司,都会问一些关于Promise的问题.如果你能根据PromiseA+的规范,写出符合规范 ...
- jQuery截取字符串的几种方式
在我们写前端JS代码的时候,我们会遇到只需要其中部分字符串的时候,下面我就提供集中截取字符串的方法: 1.取后缀 var fileDir = $("#file").val(); v ...
- 第二周java学习总结
学号 20175206 <Java程序设计>第二周学习总结 教材学习内容总结 第二章是基本数据类型与数组,第三章是运算符.表达式和语句的内容.如果说第一章是让我们了解JAVA,那么第二章和 ...
- CTF--web
https://adworld.xctf.org.cn/task/task_list?type=web&number=3&grade=0 1.view source 查看源代码 1.鼠 ...
- Java基础--面向对象编程2(封装)
1.封装的定义: 封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问. 2. 为什么需要封装?封装的作用和含义? 首先思考一个问题:当我们要 ...
- VS打开项目 提示Asp.net4.0未在web服务器上注册的解决方案
Visual Studio2013 下载的Visual Studio2013更新4 http://go.microsoft.com/fwlink/?LinkId=510314有关的Visual Stu ...
- 解析中国天气网页面获取七日天气 (Java, Python)
说明 解析中国天气网的页面,获取七日天气. 使用 htmlparser .这是它的 API 文档. 代码 SevenDayWeather.java import java.io.BufferedRea ...
- Pytorch学习笔记(一)Numpy SciPy MatPlotlib Tutorial
英文原文链接:http://cs231n.github.io/python-numpy-tutorial/ Numpy Numpy是Python中科学计算的核心库.它提供了一个高性能的多维数组对象,以 ...
- EOCS 最低资源保障机制
本期小E将为大家带来EOCS 最低资源保障机制. 为满足普通用户日常的转账等基本需求,无需再为较少的初始资源抵押担心无法使用链上功能.EOCS可以通过链的参数来调整分配给每个用户免费的资源额度,相当于 ...