云风工作室
『2002旧流言』 | 『2001旧流言』 | 『1997旧流言』

不再提供留言服务,仅能浏览旧留言。


C++对象计数

对某类对象计数有时是很有必要的, 尤其是利于调试. 因为一些内存泄露的 bug 的表象就是某些对象不停的被分配而没有释放. 对象的数目在短期膨胀到一个极大的值. 通常我们会写这样一个 template 来干这种事情:

template <typename T>
class counter {
public:
	counter() { ++_count; }
	~counter() { --_count; }
	int current_count() const { return _count; }
private:
	static int _count;
};

template<typename T> int counter<T>::_count=0;

在需要记数的 class 里放一个 counter 成员就够了 :) 例如:

class foo {
	counter<foo> _counter;
	// ...
};

然后任何时候都可以用 _counter.current_count() 取到当前对象的数目.

在完成其它功能(比如记录下高峰时对象最多的数目)前, 重新审视一下这个 template, 它有 bug 吗? 真的在任何合法的情况下都能正常工作吗? _count 有可能被减为负数吗?

回答是, 隐患是存在的! 这就是这篇文章想讨论的有趣问题. 因为 counter 没有写 copy ctor. 如果包含它的类也没有写 copy ctor, 编译器会自动产生一个. 但是, 由于 counter 本身是没有非静态成员变量的, 属于一个POD 类型. 所以它的 copy ctor 什么都不会做. 当然也不会 ++_count 了.

你可以试一下以下代码:

class foo {
public:
	counter<foo> _counter;
};

void main()
{
	foo f1;
	foo f2(f1);
	printf("%d",f2._counter.current_count());
}

答案是 1 还是 2 ?

当然如果 class foo 有自己的 copy ctor, 答案又成了正确的 2. 因为在调用 foo 的 copy ctor 之前, 会调用 counter 的默认构造函数了.

下面随便写一些有趣但不复杂的代码, 完善这个 counter, 可以使每个 counter 都把自己注册到 counter_log 中方便检阅:

class counter_data;

class counter_log {
public:
	static counter_log& instance() {
		static counter_log __inst;
		return __inst;
	}
	void add(const char *name,counter_data *cd) {
		log_[name]=cd;	
	}
	~counter_log() {}
	void output() const; // 这个函数的实现取决于实际的运用.
private:
	counter_log() {}
	std::map<const char *,counter_data *> _log;
};

class counter_data {
	int _counter;
	int _max;
public:
	counter_data(const char *name=0) {
		counter_log::instance().add(name,this);
	}
	~counter_data() {}
	void inc() { 
		if (++_counter>_max) {
			_max=_counter;
		}
	}
	void dec() {
		--_counter;
	}
	int current_count() const { return _counter; }
	int max_count() const { return _max; }
};

template <typename T>
class counter {
public:
	counter() { _data.inc(); }
	~counter() { _data.dec(); }
	counter(const counter &) { _data.inc(); }
	int current_count() const { return _data.current_count(); }
	int max_count() const { return _data.max_count(); }
private:
	static counter_data _data;
};

template<typename T> counter_data counter<T>::_data=counter_data(typeid(T).name());

回复 | (117) | 云风 | 2003-12-04 04:26:23

嘿嘿.周末去成都蹭饭 :)

回复 | (116) | 云风 | 2003-12-04 12:38:23

云风近来可有留意这个游戏,看来会红哦,这里有演示的视频http://www.avl.com.cn/game/cs/bf1942/desertcombat/update/1030-1.htm

回复 | (115) | `海风 | 2003-12-04 12:42:39

此贴被删除

此贴被删除

此贴被删除

此贴被删除

我有一个想法,云风是否也可以做一个入门教程什么的,或者出些题目,由浅入深,比方说写一个俄罗斯方块之类的题目,然后给一些类似提示的文字描述,这样或者可以帮助像我这样的想入门的人。 :) 哦,当然,在不影响你工作的前提下。

回复 | (109) | `海风 | 2003-12-02 10:13:35

此贴被删除

恭喜恭喜,不错~最近忙啥呢

回复 | (106) | Libra++ | 2003-12-01 06:30:08

云风你好,请问那个AC,DC的huffman表是怎么编出来的,我手头有DC的Huffman 表,可是那个AC的表有130那么大,麻烦你告诉我这张表是怎么算出来的,谢谢你先拉.

回复 | (104) | goldensun | 2003-12-01 10:38:07

此贴被删除

此贴被删除

此贴被删除

此贴被删除

云,知道你对于cache比较了解关于cache的可以写一篇文章介绍下吗 :)

回复 | (92) | 铁板 | 2003-11-26 05:46:29

test testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttes 『More』

回复 | (90) | snoopy | 2003-11-25 02:15:01

还没去aus啊?那天雷敬是不是去广州敲诈你请客啊?kaka....管教无方啊

回复 | (89) | shabbie | 2003-11-25 12:35:42

hihi

回复 | (88) | 胡建铸 | 2003-11-24 11:18:08

今天看了老哥的 Game 编程中毒十大症状 发现自己原来也有其中的数种,不过,还是老哥变态一点,最有同感的是最后一条,坐在计算机前需要消耗大量的这种碳酸饮料!

回复 | (87) | `海风 | 2003-11-24 10:07:39

class Kill(object):
     def __init__(self):
           self.type=1
	『More Lines』

回复 | (86) | 一刀 | 2003-11-24 05:15:26
[首页][上一页][下一页][末页]

本主页全部页面都使用文本编辑器逐行写成
手工制品,质量保证 :-)

©1997-2006 云风工作室. All rights reserved.