0

ゲームワールド内のいくつかのオブジェクトの統計エディターを作成しています。オブジェクトタイプごとに複数の編集メニューを用意するのではなく、メニューを1つだけ用意し、編集中の統計へのポインターと作業を行うための関数を含むstat-edit-objectsのリスト/ベクトルを渡します。

struct StatEditObjPureBase
{
    std::vector<std::string> reps;
    std::string name;
    std::basic_string<int> value;
    const iWindow* win;
    Editor* pE;

    StatEditObjPureBase(iWindow& rIW, Editor& rE) : win(&rIW), pE(&rE) {}

    virtual ~StatEditObjPureBase() = 0;
    virtual StatEditObjPureBase* clone() = 0;
    virtual void set() = 0;
    virtual void dec() = 0;
    virtual void inc() = 0;
    virtual void update() = 0;
};

struct StatEditObjWrap
{
    StatEditObjPureBase* base;

    StatEditObjWrap(StatEditObjPureBase* p) : base(p) {}
    StatEditObjWrap(const StatEditObjWrap& that) { base = that.base->clone(); }
    ~StatEditObjWrap() { delete base; }
};

template<class T> struct StatEditObj_INT : StatEditObjPureBase
{
    T* pMain;

    StatEditObj_INT(T* p, iWindow& rIW, Editor& rE) : StatEditObjPureBase(rIW, rE), pMain(p) {}
    StatEditObj_INT* clone() { return new StatEditObj_INT(*this); }
    void set();
    void dec(){--(*pMain);}
    void inc(){++(*pMain);}
    void update();
};

template<class T> void StatEditObj_INT<T>::set()
{
    *pMain = input_getInt(win->box_input_y, win->box_input_x, win->box_input_range, win->box_input_fg, win->box_input_bg);
}

template<class T> void StatEditObj_INT<T>::update()
{
    staticStatEditObj_intUpdate(*pMain, value, reps);
}

私の主な問題は、ポインターがテンプレート派生クラスに格納されている変数のタイプを示す必要があることです。次のコードは小さな例ですが、これらの統計編集オブジェクトエントリが数百あると想定できます。

void Editor::statEditObjectTemplate(ObjectTemplate& r)
{
    std::vector<iWindowEntry> temp;
    iWindow iw(17, 60, temp);

    std::vector<StatEditObjWrap> stats;

    { StatEditObjWrap stat(new StatEditObj_INT<unsigned short>(&r.glyph, iw, *this)); stats.push_back(stat); }
    { StatEditObjWrap stat(new StatEditObj_INT<unsigned int>(&r.mappedFcolour, iw, *this)); stats.push_back(stat); }
    { StatEditObjWrap stat(new StatEditObj_INT<unsigned int>(&r.mappedBcolour, iw, *this)); stats.push_back(stat); }

    statEditor(stats, iw);
}

テンプレートのタイプ名を取得する方法はありますか

new StatEditObj_INT<type>(&r.variable, iw, *this)

自動的に合格しましたか?

(注:typeは、StatEditObj_INTのコンストラクターの最初の引数用です)

4

2 に答える 2

2

はい、ファクトリ関数を使用できます。

// Note:  Callee takes ownership of returned pointer
// (Alternatively, you should consider using a smart pointer like shared_ptr)
template <typename T>
StatEditObj_INT<T>* MakeNew_StatEditObj_INT(T* p, iWindow& rIW, Editor& rE)
{
    return new StatEditObj_INT<T>(p, rIW, rE);
}

次に、次のようになります。

stats.push_back(MakeNew_StatEditObj_INT(&r.glyph, iw, *this));
stats.push_back(MakeNew_StatEditObj_INT(&r.mappedFcolour, iw, *this));
stats.push_back(MakeNew_StatEditObj_INT(&r.mappedBcolour, iw, *this));

これが機能するのは、関数テンプレートを使用すると、コンパイラが関数引数からテンプレート引数を推測できるためです(関数パラメータリストに表示されていないテンプレートパラメータがある場合や、推定されていないコンテキストでのみ表されます)。これは、クラステンプレートを扱うときの一般的なイディオムです。

于 2010-10-21T21:54:47.467 に答える
1

非テンプレートクラスでテンプレートメンバー関数を使用します。

struct StatEditObjWrap
{
  template <typename T>
  static StatEditObjWrap create(T* p, iWindow& rIW, Editor& rE);

  //...
}

template <typename T>
StatEditObjWrap StatEditObjWrap::create<T>(T* p, iWindow& rIW, Editor& rE) {
  return StatEditObjWrap( new StatEditObj_INT<T>( p, rIW, rE ) );
}

{
  vector<StatEditObjWrap> stats;
  stats.push_back(StatEditObjWrap::create(&r.glyph, iW, *this));
}
于 2010-10-21T21:57:32.413 に答える