And*_*nck 10
您可以在要计算的对象的析构函数和构造函数中注入代码:
SomeObject::SomeObject() {
++globalSomeObjectCounter;
}
SomeObject::~SomeObject() {
--globalSomeObjectCounter;
}
Run Code Online (Sandbox Code Playgroud)
不要忘记在所有构造函数(复制构造函数等)中增加计数器
编辑:在这种情况下,人们可以使用奇怪的重复模板模式:
template <typename T>
struct Counter
{
Counter() {++counter;}
virtual ~Counter() {--counter;}
static int counter;
};
template <typename T> int Counter<T>::counter(0);
Run Code Online (Sandbox Code Playgroud)
然后:
class SomeObject : public Counter<SomeObject> {
}
Run Code Online (Sandbox Code Playgroud)
自动为每个类类型生成一个计数器.
| 归档时间: |
|
| 查看次数: |
838 次 |
| 最近记录: |