项目A, PA.h :
#include "Common.h"
class PA
{
void func()
{
Common::getInstance()->cm();
Common::getInstance()->onlyCallByPA();
}
}
Project Common, Common.h :
class Common
{
SINGLETON
public:
void cm(){}
private:
//I do not want PB to call onlyCallByPA
//so I want to add class PA to friend class
//so I need to include PA.h
//but if I include PA.h, PB.cpp include PA.h
//this will make PA.h expose to PB
//I do not want PB to include PA.h
void onlyCallByPA(){}
}
项目B, PB.cpp :
#include "Common.h"
class PB
{
//I need to call cm() but PB do not be allowed to call onlyCallByPA
//and also do not be allowed to include PA.h
}
所以我想将PA
作为Common
的朋友班,但这会引入对PB
的依赖。
更好的解决方案?或者,我可以使用其他设计来实现我想要的吗?
答案 0 :(得分:1)
使用前瞻性声明。这将允许您声明友谊,而不包括标题或依赖PA
的完整类声明。
COMMON.H
class PA; // forward declaration.
class Common
{
friend PA;
};
答案 1 :(得分:0)
你的意思是:
friend return_type class_name::function(args);
class Common
{
// ...
friend void PA::OnlyCallByPA(); // Only PA has access
};
// EDIT
为PA创建基类,并仅在“Common.h”中引入基类