具有静态和const私有成员的奇怪重复模板模式

时间:2019-03-21 21:00:37

标签: c++ templates

我有以下模板类:

template<typename T> class virtualCard
{
private:
    static int numCards;
    const int maxCards;
public:
    virtualCard(int);  //I want to manage all card construction from here
    virtual ~virtualCard();
    virtual void activate() = 0;
};
template<typename T> int virtualCard<T>::numCards = 0;
template<typename T> virtualCard<T>::virtualCard(int max) : maxCards(max) 
{
    if (numCards < maxCards)
        ++numCards;
    else
        throw std::logic_error("All these cards are on other people's hand, you can not draw any.");
}

然后,据我了解,模板的每个实例将具有自己的numCardsmaxCards

然后,我有了这个card类,这是一个抽象类,仅保留游戏中的总牌数。

class card : public virtualCard<card>
{
public:
    card();
    virtual ~card();
    virtual void activate() = 0;
};
card::card() : virtualCard(13) {} //there is a maximum of 13 cards in-game at any given time
card::~card() {} //don't do anything, it is all managed from the template class

但是当我想创建一个给定的卡片时,我遇到了一个问题,例如:

class moveAnywhere : public card, public virtualCard<moveAnywhere>
{
public:
    moveAnywhere();
    virtual ~moveAnywhere();

    virtual void activate();
};

我应该如何称呼这张卡的构造函数?

我尝试了moveAnywhere::moveAnywhere() : card(), virtualCard(3) {}

我的意图是创建一个moveAnywhere卡会创建一个card(因此card计数器会自动增加1。此外,我将拥有自己的moveAnywhere静态通过以virtualCard<moveAnywhere>作为参数调用3来进行计数器(因为这些卡的最大值为3,card则为13)。

问题是我得到“ virtualCard的歧义访问”,我试图显式更改构造函数:

card::card() : virtualCard<card>(13) {}
moveAnywhere::moveAnywhere() : card(), virtualCard<moveAnywhere>(3) {}

但是我仍然遇到错误。

0 个答案:

没有答案