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
}

PAだから私はasCommonの友達クラスを作りたいのですが、これは への依存を導入しPBます。
より良い解決策はありますか?または、他のデザインを使用して必要なものを実装できますか?

4

2 に答える 2

1

前方宣言を使用します。これにより、ヘッダーを含めたり、の完全なクラス宣言に依存したりせずに、友情を宣言できますPA

Common.h

class PA; // forward declaration.

class Common
{
    friend PA;
};
于 2013-06-19T02:22:40.930 に答える