0

我正在为游戏世界中的某些对象创建一个统计编辑器。我没有为每种对象类型设置多个编辑菜单,而是只有一个菜单,并传入一个 stat-edit-objects 列表/向量,其中包含指向正在编辑的 stat 的指针,以及完成工作的函数。

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);
}

我的主要问题是必须指出其指针存储在模板派生类中的变量的类型。以下代码是一个小示例,但您可以假设会有数百个这样的 stat 编辑对象条目:

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

Yes, you can use a factory function:

// 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);
}

Then you have:

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));

This works because when you use a function template, the compiler can deduce the template arguments from the function arguments (there are obviously cases where it can't, like if you have a template parameter that isn't represented in the function parameter list or is only represented in a non-deduced context). This is a common idiom when dealing with class templates.

于 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 回答