c++ primer,友元函数上的一个例子(By Sybase)
本文试图解释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)相关推荐
- linux运行qt桌面生成pro怎么写,把桌面qt代码编译运行到qpe上的一个例子
qt-x11版本是2.3.2 qt core版本是2.3.2 qtopia版本是2.2.x qt-x11版本安装目录下的tutorial目录下有一些例子.我把其中的t7目录下的程序(这是一个lcd数值 ...
- c++关联容器的成员函数find的一个例子
先来一个灰色难懂的部分镇楼. #include <string> #include <iostream> #include <list> #include < ...
- SpringBoot应用和PostgreSQL数据库部署到Kubernetes上的一个例子
创建一个名为ads-app-service的服务: 上述Service的yaml文件里每个字段,在Kubernetes的API文档里有详细说明. https://kubernetes.io/docs/ ...
- C++的友元函数,友元类及#pragma once的作用
把代码都放最后吧 友元函数的作用: 可以访问该类的私有属性 但在类的外面定义的时候没有"类::" 所以该友元函数不属于该类 友元函数的意义: C++友元函数_Bussy的博客-CS ...
- C++运算符重载函数作为友元函数
运算符重载函数作为友元函数 在前面的程序例子中对运算符"+"进行了重载,使之能用于两个字符串的相加.在该例中运算符重载函数 operator +(); 作为 my_string类中 ...
- 初入c++(三)this指针,友元函数,友元类
1.c++中的this指针 指向当前对象,通过它可以访问当前对象的所有成员.当前对象就是正在使用的对象: 在类的内部使用,可以访问所有的成员,public,private,protect this只能 ...
- java有没有友元函数_c++中友元函数理解与使用
在学习c++这一块,关于友元函数和友元类,感觉还是不好理解,但是井下心来,理解,需要把我一下几点. 首先讲友元函数. (1)友元函数: 1)C++中引入友元函数,是为在该类中提供一个对外(除了他自己意 ...
- C++之友元函数和友元类讲解(一百一二十七)
目录 0.C++类中成员变量和成员函数的访问权限 <0>.C++ 类中可以有 public.protected.private 三种属性的成员. <1>.外部类可以通过实例化对 ...
- 【C++】模板类的友元函数
模板类友元函数 模板类的友元函数 参考:https://blog.csdn.net/dreamer_lhs/article/details/53580088 区分:友元是否为函数模板 非模板友元 约束 ...
最新文章
- 社区医学的研究方法:调查、流行病学研究、方案评估、临床试验Research Methods in Community Medicine: Surveys, Epidemiological Resear
- php文件解锁,php文件锁怎么用
- 插件化、热补丁中绕不开的Proguard的坑
- Amazon S3数据一致性模型
- python设计模式18-备忘录模式
- 均分纸牌模型之mxj分礼物
- db2 删除索引_程序员必须了解的知识点——你搞懂mysql索引机制了吗?
- DRY(Don't Repeat Yourself )原则
- Vue项目上线的基本流程
- 胚胎干细胞研究成果集锦,目录大合集
- 快消品行业B2B电商平台解决方案
- 解决Chrome或Microsoft Edge浏览器打开时自动跳转到hao123
- leetcode1438
- 一秒批量修改文件扩展名(后缀名)
- Monitor(管程)是什么意思?Java中Monitor(管程)的介绍
- 99的测试人还不会用nose进行自动化测试
- 送5本新出版的《剑指offer》
- LWIP (chapter 2.01) pbuf数据包缓存
- 智能手机低价成潮,vivo为何执念高端?
- 安徽省计算机学校排名,2018“中国最好学科排名”公布 安徽这14所高校上榜