クラスをネームスペース内でのみ使用できるようにすることはできますか?または、名前空間を使用せずに別の方法がありますか? 私はフレームワークの作成に取り組んでおり、このフレームワークのユーザーがすべてのクラスにアクセスできるようにする必要はありません。C++:クラスとそのデータメンバーの一部をネームスペースでのみ使用可能にする
しかし、ユーザーは、これらのクラスへのポインタ変数を作成するために、すべての定義に到達することができます。さらに、彼はこれらのクラスのすべてのデータメンバーにアクセスすることはできませんが、私のフレームワークがすべてのデータメンバーにアクセスできるようにします。
これは可能ですか?
例は、(ちょうど私の要求のための説明として):
/* T2DApp.h */
namespace T2D {
// I don't want the user to be able to create an instance of this class (only pointer vars), but the framework should be able to.
class T2DApp {
public:
// constructor, destructor... //
SDL_Window* Window;
SDL_Surface* Surface;
bool Running = false;
}
}
/* T2D.h */
#include "T2DApp.h"
void init();
/* T2D.cpp */
#include "T2D.h"
void init() {
T2D::T2DApp app; // function in framework is able to create new instance of T2DApp.
app.Window.Whatever(); // every data member should be available to framework directly without getter methods.
app.Window.Whatever(); // dito
app.Running = true; // dito
}
/* [cpp of user] */
#include "T2D.h"
void main(etc.) {
...
T2D::T2DApp app; // User shouldn't be able to create an instance of T2DApp
T2D::T2DApp* p_app; // but he should still be able to "see" the class definition for creating pointers
...
p_app.Running = true; // User shouldn't be able to access this data member
p_app.Window.Whatever(); // But he should be able to access the other data members
p_app.Surface.Whatever(); // dito
...
}
は事前にありがとうございました:)
「T2DApp」の前後に「名前空間の詳細」または「名前空間の内部」があり、「名前空間 'detail' /内部のものはマニュアルでは触れません" – Justin