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 ...
随机推荐
- iOS开发中几种常见的存储方式
1.archive 归档 数据的保存 1: let result = NSKeyedArchiver.archiveRootObject(contacts, toFile: path as Strin ...
- MythXinWCF通用宿主绿色版V1.2发布,及服务启动相关说明
最新下载地址 更新日志: 1.宿主配置增加了最大连接及队列数.允许数据传输量 2.程序大量优化 3.bug修正 4.增加已服务方式启动 点击服务方式启动后,会将软件注册为服务. 然后软件就会变成一 ...
- htmltestrunner解决错误日志出界问题
扩大背后的区域放大,让它看起来没有出界 .popup_window { display: none; position: relative; left: 0px; top: 0 ...
- JVM OQL查询语言
OQL查询语言 SELECT Clause The SELECT clause determines what to extract from the heap dump. To display ob ...
- sqlmapapi的跨域访问Access-Control-Allow-Origin:*;ajax
1.做sqlmapapi的二次开发时,需要通过ajax方式调用sqlmapapi,但是默认情况下,sqlmapapi是不允许跨域访问的 2.尝试增加ajax的header,修改origin的值,来避免 ...
- cs-Filters
ylbtech-Unitity: cs-Filters HealthcareAuthorizeAttribute.cs HealthcareHandleErrorAttribute.cs Health ...
- iOS:扩展UIColor,支持十六进制颜色设置
来自转载:http://my.oschina.net/leejan97/blog/307491 摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换 #define UICol ...
- XPath注入技术综述
一次完整的 XPath 注入攻击应该包括使用特殊构造的查询来提取一个 XML 数据库内的 数据或者信息.作为一门新的技术,XPath 注入在一定程度上和 SQL 注入漏洞有着惊人的相 似之处,通过下面 ...
- CreatarGlobe实现多机立体显示方案(初稿)
CreatarGlobe实现多机立体显示方案(初稿) 关键字 : 集群渲染 立体显示 大屏幕 边缘融合 多机同步 多机同步显示 关键字: 大屏幕投影融合系统解决方案 集群渲染 多机3D同步显示又称“集 ...
- gensim加载word2vec训练结果(bin文件)并进行相似度实验
# -*- coding: utf-8 -*- import gensim # 导入模型 model = gensim.models.KeyedVectors.load_word2vec_format ...