// templatetest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <typeinfo>
#include <string>

using namespace std;

template <typename CountedType>
class ObjectCounter{
private:
    static size_t count;
protected:
    //缺省构造函数
    ObjectCounter(){
        ++ObjectCounter<CountedType>::count;
    }

    //拷贝构造函数
    ObjectCounter(ObjectCounter const &){
        ++ObjectCounter<CountedType>::count;
    }

    //析构函数
    ~ObjectCounter(){
        --ObjectCounter<CountedType>::count;
    };

public:
    //返回存在对象的个数
    static size_t live(){
        return ObjectCounter<CountedType>::count;
    }
};

template<typename CountedType>
size_t ObjectCounter<CountedType>::count=0;

template <typename T>
class countt:public ObjectCounter<countt<T>>
{
public:
    countt()
    {}

};


int _tmain(int argc, _TCHAR* argv[])
{
    countt<int> tt;

    cout<<tt.live()<<endl;
    getchar();
    return 0;
}

相关文章:

  • 2021-05-17
  • 2021-05-15
  • 2022-02-10
  • 2021-11-18
  • 2021-07-27
  • 2022-12-23
  • 2021-12-12
  • 2021-12-04
猜你喜欢
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
  • 2021-08-24
  • 2022-12-23
  • 2021-08-06
相关资源
相似解决方案