【问题标题】:CRTP - Counting Individual Classes and Total CountCRTP - 计算单个类和总计数
【发布时间】:2014-07-31 18:35:38
【问题描述】:

我最近了解了 CRTP,并且一直在练习一些记录更好的代码示例以更好地理解它。目前我拥有的代码块能够计算不同派生类的实例,但是如果我想找到所有实例的总数而不考虑类怎么办?这是我的代码:

template <typename CountedType>
class countable {
    static unsigned count_;
public:
    countable() { ++count_; }
    countable(countable const&) { ++count_; }
    ~countable() { --count_; }

    static unsigned n_alive() { return count_; }

};
template <typename CountedType>
unsigned countable<CountedType>::count_ = 0;

template <typename CharT>
class BasicMyString : public basic_string < CharT >, public countable<BasicMyString<CharT>> { };


typedef BasicMyString<char> MyString;
typedef BasicMyString<wchar_t> WMyString;

template <typename T>
class MyVector : public vector<T>, public countable<MyVector<T>> {};

int main() {
    MyString s1, s2;
    WMyString wms;
    MyVector<double> v;

    cout << "Number of MyString: " << MyString::n_alive() << endl;
    cout << "Number of WMyString: " << WMyString::n_alive() << endl;
    cout << "Number of MyVector: " << MyVector<double>::n_alive() << endl;
   //cout << "Total number of objects: " << countable::n_alive() << endl;
}

当前注释掉的最后一行是我希望能够调用以查看当前总共有多少对象的方式。我知道我可以通过创建一个全局计数器并在类中创建一个新函数来返回该全局计数来找到总数,但我希望能够像这样调用它,而无需定义模板化参数。我是否必须制作一个适配器类型的接口,还是有更简单的方法来实现它?

谢谢。

【问题讨论】:

  • 将countable设为非模板类​​的派生类,如countable_base,并将所有计数代码移至基类中。
  • 如果你这样做,countable 就没有用了,不是吗?
  • 太棒了,@RSahu,如果你想把它作为答案,我会继续接受!

标签: c++ crtp


【解决方案1】:

解决方案 1

将countable设为非模板类​​的派生类,如countable_base,并将所有计数代码移至基类。

class countable_base {
    static unsigned count_;
public:
    countable_base() { ++count_; }
    countable_base(countable_base const&) { ++count_; }
    ~countable_base() { --count_; }

    static unsigned n_alive() { return count_; }

};
unsigned countable_base::count_ = 0;

template <typename CountedType>
class countable : public countable_base {};

解决方案 2

让countable 成为普通班级。

class countable {
    static unsigned count_;
public:
    countable() { ++count_; }
    countable(countable const&) { ++count_; }
    ~countable() { --count_; }

    static unsigned n_alive() { return count_; }

};
unsigned countable::count_ = 0;

【讨论】:

  • 作为状态,OP 按类型松散计数。
【解决方案2】:

您可以创建一个额外的类来进行计数,这将是您当前模板countable 类的基础,例如:

class countable_total {
    static unsigned count_;
protected:
    countable_total() { ++count_; }
    countable_total(countable_total const&) { ++count_; }
    ~countable_total() { --count_; }
public:
    static unsigned n_alive() { return count_; }
};

template <typename CountedType>
class countable : public countable_total {
    static unsigned count_;
public:
    countable() { ++count_; }
    countable(countable const&) { ++count_; }
    ~countable() { --count_; }

    static unsigned n_alive() { return count_; }
};

template <typename CountedType>
unsigned countable<CountedType>::count_ = 0;

// in cpp
unsigned countable_total::count_ = 0;

【讨论】:

    猜你喜欢
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多