0

構成ファイルを処理するためのクラスを作成しています。私がそれを機能させたい方法は次のとおりです。

すべての操作 (データの挿入、データの消去など) を保持するキューがあり、メソッドCommitはそれらすべての変更を適用します。

私のプロトタイプは次のようになります (ファイルのすべての設定は STL マップに保存されます)。

bool Insert(std::string const& key, std::string const& value);
bool Erase(std::string const& key);

それで、関数へのポインタのSTLキューを作成しようとしましたが、関数には同じ数の引数がなく、どうすればよいかわかりません...

4

1 に答える 1

2

Operation 基本クラスを作成し、それを 2 回派生させて Insert クラスと Erase クラスを作成できます。その後、Operation クラスへのポインターを格納できます。

class Settings
{
public:
    bool Insert(std::string const& key, std::string const& value)
    {
        m_operations.push_back(new Insert(key, value));
        return true; //?      
    }

    bool Erase(std::string const& key)
    {
        m_operations.push_back(new Erase(key));
        return true; //?
    }

    bool Commit()
    {
        // You know what to do by now right ?
    }

private:
    class Operation
    {
    public:
        virtual void Execute() = 0
    }

    class Insert : public Operation
    {
    public:
        Insert(std::string const& key, std::string const& value) : 
            m_key(key), m_value(value) {}

        void Execute() {...}
    private:
        std::string m_key;
        std::string m_value;
    }

    class Erase : public Operation
    {
    public:
        Erase(std::string const& key) : m_key(key) {}

        void Execute() {...}
    private:
        std::string m_key;
    }

    std::queue<Operation*> m_operations;
    std::map<std::string, std::string> m_settings;
}

次に、操作クラスを設定クラスのフレンドにするか、操作が適用されるマップに参照を渡します。

于 2010-12-17T01:39:38.080 に答える