2017-06-19 5 views
-1

私は、自分のknoledgeの最高の状態機械の例であるいくつかのコードを書こうとしています。私がこれまで行ってきた何C++ステートマシンとファイル内の隠蔽クラス

は次のようになります。これは、しかし、私は単一の状態を存在するがしたい私のプログラムの実行時に、正常に動作します

#include <map> 
#include <iostream> 

class StateInstance 
{ 
    std::string m_string; 

public: 

    StateInstance(const std::string& string) 
     : m_string{string} 
    { 
    } 

    std::string Get() const 
    { 
     return m_string; 
    } 
}instance_a("hello world"), instance_b("bring me coffee"); 

enum class StateInstanceOption 
{ 
    STATE_INSTANCE_A, 
    STATE_INSTANCE_B 
}gCurrentState{StateInstanceOption::STATE_INSTANCE_A}; // global variable to hold current state "pointer" (really a flag) 

class StateInstanceMapper 
{ 
    std::map<StateInstanceOption, const StateInstance&> m_map; 

public: 

    StateInstanceMapper() 
    { 
     m_map.insert(std::pair<StateInstanceOption, const StateInstance&>(StateInstanceOption::STATE_INSTANCE_A, instance_a)); 
     m_map.insert(std::pair<StateInstanceOption, const StateInstance&>(StateInstanceOption::STATE_INSTANCE_B, instance_b)); 
    } 

    const StateInstance& DoMap(/*const StateInstanceOption opt*/) const 
    { 
     return m_map.at(/*opt*/ gCurrentState); 
    } 

}mapper_instance; 

int main() 
{ 

    std::cout << mapper_instance.DoMap(/*gCurrentState*/).Get() << std::endl; 

    gCurrentState = StateInstanceOption::STATE_INSTANCE_B; 

    std::cout << mapper_instance.DoMap(/*gCurrentState*/).Get() << std::endl; 

    return 0; 
} 

  • 私は(Cで)、機能の例についてはStateInstanceStateInstanceOptionStateInstanceMapper

の別のインスタンスを作成し、ユーザーを防止するための方法をしたい、私は関数は、静的行い、入れるでしょうそれは別のファイルには見えないようにすることです。

エンドユーザーがこれらのオブジェクトのインスタンスをさらに作成するのを止めるためにできることはありますか?

あなたが作成しようとしている何

#include <map> 
#include <iostream> 

class StateInstance 
{ 
    std::string m_string; 

public: 

    // somewhat inconveniently, have to create an entirely new function for each possible instance of the state 
    static StateInstance& get_instance_a() 
    { 
     static StateInstance instance_a("hello world"); 
     return instance_a; 
    } 
    static StateInstance& get_instance_b() 
    { 
     static StateInstance instance_b("bring me coffee"); 
     return instance_b; 
    } 

private: 

    StateInstance(const std::string& string) 
     : m_string{string} 
    { 
    } 

    StateInstance(const StateInstance &) = delete; 
    StateInstance& operator=(const StateInstance&) = delete; 

public: 

    std::string Get() const 
    { 
     return m_string; 
    } 
}; // instance_a("hello world"), instance_b("bring me coffee"); 

enum class StateInstanceOption 
{ 
    STATE_INSTANCE_A, 
    STATE_INSTANCE_B 
}gCurrentState{StateInstanceOption::STATE_INSTANCE_A}; // global variable to hold current state "pointer" (really a flag) 

class StateInstanceMapper 
{ 
    std::map<StateInstanceOption, const StateInstance&> m_map; 

public: 

    static StateInstanceMapper& getInstance() 
    { 
     static StateInstanceMapper instance; 
     return instance; 
    } 

private: 

    StateInstanceMapper() 
    { 
     m_map.insert(std::pair<StateInstanceOption, const StateInstance&>(StateInstanceOption::STATE_INSTANCE_A, StateInstance::get_instance_a())); 
     m_map.insert(std::pair<StateInstanceOption, const StateInstance&>(StateInstanceOption::STATE_INSTANCE_B, StateInstance::get_instance_b())); 
    } 

    StateInstanceMapper(const StateInstanceMapper &) = delete; 
    StateInstanceMapper& operator=(const StateInstanceMapper &) = delete; 

public: 

    const StateInstance& DoMap(/*const StateInstanceOption opt*/) const 
    { 
     return m_map.at(/*opt*/ gCurrentState); 
    } 

}; //mapper_instance; 

int main() 
{ 

    //std::cout << mapper_instance.DoMap(/*gCurrentState*/).Get() << std::endl; 
    std::cout << StateInstanceMapper::getInstance().DoMap().Get() << std::endl; 

    gCurrentState = StateInstanceOption::STATE_INSTANCE_B; 

    //std::cout << mapper_instance.DoMap(/*gCurrentState*/).Get() << std::endl; 
    std::cout << StateInstanceMapper::getInstance().DoMap().Get() << std::endl; 

    return 0; 
} 
+3

ので、あなたがシングルトンをしたいですか? – vu1p3n0x

+0

これは何が有望なのでしょうか? – user3728501

+0

_ "有望な音" _ - 私は、音の下痢について同じことを考えました。 –

答えて

-1

「この質問に対する答えは」singletonで、この質問が停止でそうrappidly、私は答えとして答えを追加することはできません閉じたので、しかしここで、作りますコンストラクタprivate、およびシングルトンインスタンスを提供する関数を提供します。 C++でのシングルトンの例として

class Singleton { 
    std::string message; 

    // Private constructor 
    Singleton(const std::string& s) : message(s) {} 

public: 
    static Singleton& instance() { 
    static Singleton s ("Hello!"); 
    return s; 
    } 

    const std::string& get_message() { 
    return message; 
    } 
} 
+0

これに第2の部分は何ですか?コンストラクタがプライベートである場合、どのように動作しますか? – user3728501

+0

コンストラクタがprivateの場合、クラス内でのみコンストラクタを使用できます。クラスはインスタンスを作成する静的関数を提供できます。 – Nullrelation

+0

どこから静的関数を呼び出しますか? – user3728501

関連する問題