2

我不确定这是否可以做到,我只是深入研究模板,所以也许我的理解有点错误。

我有一个士兵排,排从一个编队继承来获取编队属性,但是因为我可以拥有尽可能多的编队,所以我选择使用CRTP来创建编队,希望我可以制作一个用于存储排的向量或排数组。但是,当然,当我制作排时,它不会将其存储在向量中,“类型不相关”

有没有办法解决 ?我读到了类似的“单板”,它们与数组一起工作,但我无法让它工作,也许我错过了一些东西。

这是一些代码:(对不起格式,代码在我的帖子中,但由于某种原因没有显示)

template < class TBase >
class IFormation 
{
public : 
~IFormation(){}

bool IsFull()
{
    return m_uiMaxMembers == m_uiCurrentMemCount;
}
protected:
    unsigned int m_uiCurrentMemCount;
    unsigned int m_uiMaxMembers;
    IFormation( unsigned int _uiMaxMembers  ): m_uiMaxMembers( _uiMaxMembers ), m_uiCurrentMemCount( 0 ){}      // only allow use as a base class.

    void SetupFormation( std::vector<MySoldier*>& _soldierList ){}; // must be implemented in derived class
};
/////////////////////////////////////////////////////////////////////////////////
// PHALANX FORMATION
class Phalanx : public IFormation<Phalanx>
{
public:
Phalanx( ):
  IFormation( 12 ),
  m_fDistance( 4.0f )
{}

~Phalanx(){}


protected:
float   m_fDistance;        // the distance between soldiers
void    SetupFormation( std::vector<MySoldier*>& _soldierList );
};
///////////////////////////////////////////////////////////////////////////////////
// COLUMN FORMATINO
class Column : public IFormation< Column >
{
public :
Column( int _numOfMembers ):
   IFormation( _numOfMembers )
   {}

~Column();
protected:
void    SetupFormation( std::vector<MySoldier*>& _soldierList );
};

然后我在 platoon 类中使用这些编队进行派生,以便 platoon 获取相关的 SetupFormation() 函数:

template < class Formation >
class Platoon : public Formation
{
public:
**** platoon code here
};

到目前为止,一切都很好,并且符合预期。

现在,由于我的将军可以有多个排,我需要存储排。

typedef Platoon< IFormation<> > TPlatoon; // FAIL
typedef std::vector<TPlatoon*>  TPlatoons;
TPlatoon            m_pPlatoons

m_pPlatoons.push_back( new Platoon<Phalanx> ); // FAIL, types unrelated.

typedef 排< IFormation<> > TPlatoon; 失败,因为我需要指定一个模板参数,但指定它只会让我存储使用相同模板参数创建的排。

所以我然后创建了FormationBase

class FormationBase
{
public:
    virtual bool IsFull() = 0;
    virtual void SetupFormation( std::vector<MySoldier*>& _soldierList ) = 0;
};

并使 IFormation 公开继承,然后将 typedef 更改为

typedef Platoon< IFormation< FormationBase > > TPlatoon;

但还是没有爱。

现在在我的搜索中,我没有找到表明这是可能或不可能的信息。

4

1 回答 1

2

C++ does not allow simple use of compile time and run time polymorphism. You are correct in that your vector is only able to hold one type. You're going to have to have your vector store a pointer to a non template type, or change the design to not use a vector.

If you want to have the CRTP'd classes publicly inherit from FormationBase, then the vector would have to be a std::vector<FormationBase *>. It's impossible to go back from the runtime class FormationBase to the compile time template parameters it was instantiated with.

Considering your data looks relatively consistent and you merely want to change how your algorithm arranges the soldiers and units on the battlefield, I'd consider using the strategy pattern to specify the SetupFormation function, and have a separate, nonpolymorphic class using that which you store in the vector.

于 2010-06-15T02:46:40.307 回答