c++ two classes as each others' friends
In this case, Box need access to Cup.func, AND Cup need access to Box.func, both of which are private because I don't want any other class to have access to neither Box.func nor Cup.func.
So they need to declare each other as friend.
Suppose you organise code like Box1.h, Box1.cpp, Cup1.h, Cup1.cpp, it would cause a problem like this: http://www.cnblogs.com/qrlozte/p/4099032.html
The compiler will complain. So the solution is also simple, see Cup2.h, Cup2.cpp, other files remain unchanged. (Of couse, you can change Box1.h Box1.cpp using the same pattern as Cup2.h Cup2.cpp, although it would make your code looks more consistent, but only changing Cup1.h, Cup1.cpp is already enough to satisfy your need.)
Another question, what if Box.doSomething is a function template? If you change main1.cpp to main2.cpp, and Box1.h, Box1.cpp to Box2.h, Box2.cpp, the linker will complain that it cannot find the implementation of Box.doSomething (undefined reference error) (after all, the compiler need all the detail of a template to generate code, if you hide the implementation of Box.doSomething in Box2.cpp, how can you expect the compiler can generate code for Box.doSomething<int>() in main2.cpp when main2.cpp doesn't include Box2.cpp, right?).
So, because function template (and class template) must define (just declare is NOT enough) in the header, you might modify Box2.h and Box2.cpp as shown in Box3.h and Box3.cpp. And you'll find the problem is solved! (Now we got main2.cpp, Box3.h, Box3.cpp, Cup2.h, Cup2.cpp compiled successfully).
Now consider what if Cup.doSomething also need to be a function template? (The same as Box.doSomething in Box3.h). And... yes, you have to include the definition of Cup.doSomething in Cup.h, too!
And you if you that, the compiler will complain Box is an incomplete type thus in Cup.doSomething, "b.func" cannot be resolved. As shown in main3.cpp, Cup3.h, Cup3.cpp.
The reason is because in Cup3.h "class Box;" it indeed declares class Box, but just the class name, no information is provided about the interface of the class at all.
Can you replace "class Box;" with "#include "Box.h"" ? No. Because that'll cause the same problem as Box1.h, Box1.cpp, Cup1.h, Cup1.cpp caused.
The solution ? At least for now, I don't know(If I can seperate Cup.doSomething() into two parts, one part is template and one part is normal function and the template part doesn't have to access memebers of the parameter 'b', then there's a simple solution, just let the template part in the header, and implement the normal function in Cup.cpp). This situation is not made up by me, I really encountered this problem when I was making a little program. Maybe there's something wrong with my design.
main1.cpp
#include "Box.h"
#include "Cup.h" int main()
{
Cup c;
Box b;
c.doSomething(b);
b.doSomething(c);
return ;
}
Box1.h
#ifndef BOX_H
#define BOX_H #include "Cup.h" class Box
{
friend class Cup;
public:
Box();
~Box();
void doSomething(const Cup &c);
private:
void func() const;// only visible to Cup
}; #endif // BOX_H
Box1.cpp
#include "Box.h" #include <iostream> Box::Box()
{ }
Box::~Box()
{ }
void Box::doSomething(const Cup &c)
{
c.func();
}
void Box::func() const
{
using namespace std;
cout << "Box.func" << endl;
}
Cup1.h
#ifndef CUP_H
#define CUP_H #include "Box.h" class Cup
{
friend class Box;
public:
Cup();
~Cup();
void doSomething(const Box &b);
private:
void func() const; // only visible to Box
}; #endif // CUP_H
Cup1.cpp
#include "Cup.h" #include <iostream> Cup::Cup()
{ }
Cup::~Cup()
{ }
void Cup::doSomething(const Box &b)
{
b.func();
} void Cup::func() const
{
using namespace std;
cout << "Cup.func" << endl;
}
Cup2.h
#ifndef CUP_H
#define CUP_H class Box;
class Cup
{
friend class Box;
public:
Cup();
~Cup();
void doSomething(const Box &b);
private:
void func() const; // only visible to Box
}; #endif // CUP_H
Cup2.cpp
#include "Cup.h" #include "Box.h" #include <iostream> Cup::Cup()
{ }
Cup::~Cup()
{ }
void Cup::doSomething(const Box &b)
{
b.func();
} void Cup::func() const
{
using namespace std;
cout << "Cup.func" << endl;
}
main2.cpp
#include "Box.h"
#include "Cup.h" int main()
{
Cup c;
Box b;
c.doSomething(b);
b.doSomething<int>(, c);
return ;
}
Box2.h
#ifndef BOX_H
#define BOX_H #include "Cup.h" class Box
{
friend class Cup;
public:
Box();
~Box();
template <typename T> void doSomething(const T &obj, const Cup &c);
private:
void func() const;// only visible to Cup
}; #endif // BOX_H
Box2.cpp
#include "Box.h" #include <iostream> Box::Box()
{ }
Box::~Box()
{ }
template <typename T> void doSomething(const T &obj, const Cup &c)
{
c.func();
}
void Box::func() const
{
using namespace std;
cout << "Box.func" << endl;
}
Box3.h
#ifndef BOX_H
#define BOX_H #include "Cup.h" class Box
{
friend class Cup;
public:
Box();
~Box();
template <typename T> void doSomething(const T &obj, const Cup &c);
private:
void func() const;// only visible to Cup
}; template <typename T> void Box::doSomething(const T &obj, const Cup &c)
{
c.func();
} #endif // BOX_H
Box3.cpp
#include "Box.h" #include <iostream> Box::Box()
{ }
Box::~Box()
{ } void Box::func() const
{
using namespace std;
cout << "Box.func" << endl;
}
main3.cpp
#include "Box.h"
#include "Cup.h" int main()
{
Cup c;
Box b;
c.doSomething<int>(, b);
b.doSomething<int>(, c);
return ;
}
Cup3.h
#ifndef CUP_H
#define CUP_H class Box; class Cup
{
friend class Box;
public:
Cup();
~Cup();
template <typename T> void doSomething(const T &obj, const Box &b);
private:
void func() const; // only visible to Box
}; template <typename T> void doSomething(const T &obj, const Box &b)
{
b.func();
} #endif // CUP_H
Cup3.cpp
#include "Cup.h" #include "Box.h" #include <iostream> Cup::Cup()
{ }
Cup::~Cup()
{ } void Cup::func() const
{
using namespace std;
cout << "Cup.func" << endl;
}
c++ two classes as each others' friends的更多相关文章
- 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)
坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...
- eclipse中的classes文件夹同步问题
问题: 在同步项目时,由于误操作将classes文件夹加入到了同步版本中,这样会导致每次更新程序编译后,会有很多class文件显示在同步清单中. 解决方案: 将classes文件不设置为同步. 1. ...
- Introduction of OpenCascade Foundation Classes
Introduction of OpenCascade Foundation Classes Open CASCADE基础类简介 eryar@163.com 一.简介 1. 基础类概述 Foundat ...
- 6.Configure Domain Classes(配置领域类)【EF Code-First 系列】
在前面的部分中,我们学习了Code-First默认约定,Code-First使用默认的约定,根据你的领域类,然后生成概念模型. Code-First模式,发起了一种编程模式:约定大于配置.这也就是说, ...
- app:clean classes Exception
Error:Execution failed for task ':app:clean'.> Unable to delete directory: C:\Users\LiuZhen\Deskt ...
- Android framework编译出来的jar包classes.jar的位置
在源码环境下编译 Android framework编译出来的jar包classes.jar的位置 out/target/common/obj/JAVA_LIBRARIES/framework_in ...
- yii 核心类classes.php详解(持续更新中...)
classes.php在yii运行的时候将被自动加载,位于yii2文件夹底下. <?php /** * Yii core class map. * * This file is automati ...
- Top 15 Java Utility Classes
In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...
- Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法(转载)
文章来源:http://www.cnblogs.com/xfiver/archive/2010/07/07/1772764.html 1. IOException parsing XML docum ...
- [Android] 升级了新的android studio之后 发生如下的报错,The following classes could not be instantiated:
The following classes could not be instantiated:- android.support.v4.widget.DrawerLayout (Open Class ...
随机推荐
- Docker时间和宿主同步
通过date命令查看时间 查看主机时间 [root@localhost ~]# date 2016年 07月 27日 星期三 22:42:44 CST 查看容器时间 root@b43340ecf5ef ...
- 关于ArrayList的一些源码分析
集合是Java中非常重要而且基础的内容,因为任何数据必不可少的就是该数据是如何存储的,集合的作用就是以一定的方式组织.存储数据.这里写的集合,一部分是比较常见的.一部分是不常用但是我个人平时见到过的, ...
- vulkan
https://gfxbench.com/device.jsp?benchmark=gfx40&os=Android&api=gl&D=Asus+ZenFone+4+%28Ad ...
- [转载]Oracle批量执行
FROM: http://www.cnblogs.com/wangyayun/p/4514411.html //批量添加20000条数据用时8秒. try { String url = "j ...
- jQuery 全选 正反选
<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript&quo ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-PLC支持哪些PLC语言类型
PLC的标准化语言,统称为IEC 61131-3,该规范下有五种不同的语言可以创建PLC程序,TwinCAT都支持. IL(指令列表): 每条指令都从一个新行开始并包含一个操作和一个或多个操作数,一条 ...
- 在对方电脑建立IPC连接, 利用IPC$入侵 运行木马
第一大步: IPC漏洞的建立 1)在目标主机上设置组策略:開始->执行-〉gpedit.msc 2)计算机配置->windows配置-〉本地策略-〉安全选项 3)在安全选项中, 将网络訪 ...
- c++ 银行管理系统及报告
1.题目描写叙述: 本代码为银行管理系统,总体分为管理员模式和普通用户模式: (1)在管理员模式中能完毕 ①用户信息录入 ②改动管理员password ③改动指定账户信息 ④信息管理业务 (2)在普通 ...
- 基于MaterialDesign设计风格的妹纸app的简单实现
代码地址如下:http://www.demodashi.com/demo/11644.html *今天看了郭神的第二行代码,深深的被MaterialDesign的设计风格所吸引,然后就照例做了一个小D ...
- 【MyBatis学习03】原始dao开发方法及其弊端
上一篇博文总结了一下mybatis的入门,接下来就要开发dao方法了,这篇博文主要总结一下mybatis中原始dao开发的方法,最后并总结一下原始dao开发方法的弊端.mybatis中dao开发应该使 ...