标签:style blog http io ar os sp strong on
#include<stddef.h>
#include<iostream>
template<typename CountedType>
class ObjectCounter{
static size_t count;
protected:
ObjectCounter(){ ++ObjectCounter<CountedType>::count; } //声明为protected,防止生成对象,限定只能被继承
ObjectCounter(const ObjectCounter<CountedType>& ){ ++ObjectCounter<CountedType>::count; }
~ObjectCounter(){ --count; }
public:
static size_t getCount(){ return ObjectCounter<CountedType>::count; }//作为静态函数,类方法
};
template<typename CountedType>
size_t ObjectCounter<CountedType>::count = 0;
template<typename T>
class MyString : public ObjectCounter<MyString<T> >{}; //CRTP
标签:style blog http io ar os sp strong on
原文地址:http://www.cnblogs.com/claruarius/p/4111884.html