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 ...
随机推荐
- Problem T: 零起点学算法15——交换变量
#include<stdio.h> int main() { int a,b,c; scanf("%d %d",&a,&b); c=a; a=b; b= ...
- python的turtle模块画折线图
代码如下: import turtle yValues = [10.0,7.4,6.4,5.3,4.4,3.7,2.6] def main(): t = turtle.Turtle() t.hidet ...
- Windows 无法验证此设备所需的驱动程序的数字签名。某软件或硬件最近有所更改,可能安装了签名错误或损毁的文件,或者安装的文件可能是来路不明的恶意软件。(代码52)
由未签名驱动导致的键鼠装无法使用的问题 usb 问题失效. 要是win 10的话 导致的结果就是 无线键鼠套装无法使用. 解决办法是 1.按下shift 按键 点击重启按钮 重启后 2.疑难解答-- ...
- [bug] VS2013 Brower Link和Aspnetpager引发的问题分析
概述 在ie11上浏览页面的时候,突然发现在使用Aspnetpager的页面会有一个bug. 开发环境:win8.1+vs2013+ie11. 项目描述:这个问题出现在内容页中,应用了母版页. 解决方 ...
- jquery--动态篇
jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元素的st ...
- 一些WCF DS 的资料(参考)
0.WCF DS 官方博客:http://blogs.msdn.com/b/astoriateam 1. 官方博客: 如何使用WCF DS5 由于V5 的程序集单独出来了! (How to ...
- java源码阅读HashSet
1类签名与注解 public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneabl ...
- N的阶层(王道)
题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可能包括多组数据,对于每一组输入数据,输出N的阶乘 样例输入: 4 5 15 样例输出: ...
- Spark下载与入门(Spark自学二)
2.1 下载Spark 略 2.2 Spark中Python和Scala的shell Spark shell可用来与分布式存储在许多机器的内存或者硬盘上的数据进行交互,并且处理过程的分发由Spark自 ...
- ns3加入模块之vanet-highway
在网上搜了下加入模块的教程,搜到了一个帖子:ns3加入模块 ,可是这个帖子只给出了如何加入的步骤.我认为写的比較简单,当我们须要加入别的模块时就不知道如何操作了. 所以,我写这个帖子,会将非常多遇到的 ...