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的更多相关文章

  1. 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)

    坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...

  2. eclipse中的classes文件夹同步问题

    问题: 在同步项目时,由于误操作将classes文件夹加入到了同步版本中,这样会导致每次更新程序编译后,会有很多class文件显示在同步清单中. 解决方案: 将classes文件不设置为同步. 1. ...

  3. Introduction of OpenCascade Foundation Classes

    Introduction of OpenCascade Foundation Classes Open CASCADE基础类简介 eryar@163.com 一.简介 1. 基础类概述 Foundat ...

  4. 6.Configure Domain Classes(配置领域类)【EF Code-First 系列】

    在前面的部分中,我们学习了Code-First默认约定,Code-First使用默认的约定,根据你的领域类,然后生成概念模型. Code-First模式,发起了一种编程模式:约定大于配置.这也就是说, ...

  5. app:clean classes Exception

    Error:Execution failed for task ':app:clean'.> Unable to delete directory: C:\Users\LiuZhen\Deskt ...

  6. Android framework编译出来的jar包classes.jar的位置

    在源码环境下编译 Android framework编译出来的jar包classes.jar的位置  out/target/common/obj/JAVA_LIBRARIES/framework_in ...

  7. yii 核心类classes.php详解(持续更新中...)

    classes.php在yii运行的时候将被自动加载,位于yii2文件夹底下. <?php /** * Yii core class map. * * This file is automati ...

  8. Top 15 Java Utility Classes

    In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...

  9. Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法(转载)

    文章来源:http://www.cnblogs.com/xfiver/archive/2010/07/07/1772764.html 1.  IOException parsing XML docum ...

  10. [Android] 升级了新的android studio之后 发生如下的报错,The following classes could not be instantiated:

    The following classes could not be instantiated:- android.support.v4.widget.DrawerLayout (Open Class ...

随机推荐

  1. 【动态规划】bzoj1270 [BeijingWc2008]雷涛的小猫

    暴力dp是n^2*m的……我们计算每棵树在每层的答案的时候,都需要计算出从那棵树转移过来最优. 但是我们发现,对一棵树而言,从上面转移过来都是一样的,所以我们可以在计算每棵树在每层的答案的时候,先预处 ...

  2. java 面向接口编程的理解

    初学者可能在学习中会有很多疑惑,为什么要这样,明明可以那样实现,这样做的好处又是什么? 可能会的人觉得很简单很容易理解,甚至可能觉得问的问题很智障,但对于小白来说可能是苦思冥想都不得其解的. 自己身为 ...

  3. (转)[Unity3D]关于Assets资源目录结构管理

    分享个我们项目常用的目录结构,微调过很多次,最终到了这个版本.个人认为这种管理资源方式是不错的.欢迎探讨各个细节~ 更新于2013.5.30   Asserts   --Editor 自写的灵活方便插 ...

  4. Oracle常见故障问题

    1. ORA-27102: out of memory 创建pfile文件: create pfile from spfile: 修改pfile文件 修改文件/home/oracle/app/orac ...

  5. 求斐波那契数的python语言实现---递归和迭代

    迭代实现如下: def fab(n): n1 = 1 n2 = 1 if n<1: print("输入有误!") return -1 while (n-2)>0: n3 ...

  6. freedom isn't free

    财务自由(除去房和车) 第一阶段: 个人存款达到50万以上 第二阶段 个人存款100~200万 第三阶段 个人存款400万以上 第三阶段以上才能算实现了相对较好的财务自由!come on , boys ...

  7. web 中加载配置文件

    1.web.xml中配置   <!-- 加载配置文件 -->   <listener>      <description>ServletContextListen ...

  8. Install and Enable Telnet server in Ubuntu Linux

    转:http://ubuntuguide.net/install-and-enable-telnet-server-in-ubuntu-linux 参考:http://auxnet.org/index ...

  9. Java使用纯真IP库获取IP对应省份和城市

    原文:http://blog.csdn.net/chwshuang/article/details/78027873?locationNum=10&fps=1 Java使用纯真IP库获取IP对 ...

  10. spring融合activitymq-all启动报错的解决办法

    报错信息: nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.Annotated ...