0

项目 A,PA.h

#include "Common.h"

class PA
{
   void func()
   {
     Common::getInstance()->cm();
     Common::getInstance()->onlyCallByPA();
   }
}

项目通用,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
}

所以我想做PAasCommon的朋友班,但这会引入依赖PB
有更好的解决方案吗?或者,我可以使用其他设计来实现我想要的吗?

4

2 回答 2

1

使用前向声明。这将允许您在不包括标题或依赖完整类声明的情况下声明友谊PA

通用.h

class PA; // forward declaration.

class Common
{
    friend PA;
};
于 2013-06-19T02:22:40.930 回答
0

你是这个意思:

friend return_type class_name::function(args);

class Common 
{

    // ...

    friend void PA::OnlyCallByPA(); // Only PA has access
};

//编辑

为PA做一个基类,只在“Common.h”中引入基类

于 2013-06-19T02:05:15.743 回答