本文目的是实现一个实用的对C++(www.cppentry.com)类计数的类,同时在实现过程中指出一些轻易为人忽视的C++(www.cppentry.com)知识.
要实现一个类的对象(实例)计数,即程序运行中此类有多少个对象存在,最轻易的实现方法是使用静态数据成员.
如下:
class Widget { public: Widget() { ++count;
} Widget(const Widget&) { ++count;
} ~Widget() { --count;
} static size_t howMany() { return count;
} PRivate: static size_t count;
};
//cpp文件中 size_t Widget::count = 0;
注重构造函数也要增加计数,这一点很多人轻易忘记.
但是假如程序中有多个需要实例计数的类,则在每个类中加入上面代码未免繁琐、易错.这种情况下,最好是实现一个通用计数类.它应该具备一下特点:
易于使用:任何需要计数的类(以下简称客户类)只要添加少数代码即可使用;
有效率:不增加客户类大小,对客户类性能没有影响;
健壮:客户类使用时,不轻易误用.
下面我们将逐步实现并完善这个通用的计数类.
class Counter { public: Counter() { ++count;
} Counter(const Counter&) { ++count;
} ~Counter() { --count;
} static size_t howMany() { return count;
} private: static size_t count;
}; // This still goes in an implementation file size_t Counter::count = 0;
上面这个Counter类能否正确完成计数呢 例如:Widget类利用它来进行实例计数:
// embed a Counter to count objects class Widget { public: .....
// all the usual public //
Widget stuff static size_t howMany()
{ return Counter::howMany();
} private: .....
// all the usual private
// Widget stuff Counter c;
};
//or:
// inherit from Counter to count objects class Widget: public Counter { .....
// all the usual public // Widget stuff private: .....
// all the usual private // Widget stuff };
对于Widget本身来说,Counter完成了任务.然而,假如我们在同一进程中还需要利用Counter来计数Fish类,显然,Counter就不能胜任,因为它只有一个静态成员变量,它会将Widget和Fish的个数一起统计.这个方案不行,怎么办 用模板!如下:
template<typename T> class Counter {
public: Counter() { ++count;
} Counter(const Counter&) { ++count;
} ~Counter() { --count;
} static size_t howMany() { return count;
} private: static size_t count;
};
// this now can go in header template<typename T> size_t Counter<T>::count = 0;
则上面的实现变成: // embed a Counter to count objects class Widget { public: ..... static size_t howMany() {return Counter<Widget>::howMany();} private: ..... Counter<Widget> c; }; //or: // inherit from Counter to count objects class Widget: public Counter<Widget> { ..... };
这样,其他类就可以使用Counter计数自己的实例了,它们将互不影响.
上面两种方案都可正确实现计数,我们继续探讨这两种方案的优缺点.
首先讲public继续,即class Widget: public Counter<Widget>这种方案:有经验的读者肯定会想到基类Counter的析构函数要变为虚函数.否则通过基类指针delete派生类时,结果未定义(可能导致程序crash或其他) Counter<Widget> *pw = new Widget; // get base class ptr to derived class object ...... delete pw; // yields undefined results if the base class lacks a virtual destrUCtor
但一旦Counter有虚析构函数,就会给类带入vTable,多占用了空间并影响客户类的效率.解决方法可以是将析构函数作为protected成员.这样就不能delete pw,因为它会导致编译错误. template<typename T>
class Counter { public: ..... protected: ~Counter() { --count; } ..... };
其次,Counter作为客户类的成员变量这种方案(这时Counter的析构函数必须public).一个明显的缺点是客户类必须定义Counter为其成员变量同时还得定义一个inline函数以调用Counter类得HowMany函数.另一个较隐蔽的缺点:它增大了客户类所占用的内存.Counter类没有非静态成员变量,有人就可能会认为Counter对象的大小为0,其实不然,C++(www.cppentry.com)规定所有对象的大小最小必须为1字节.所以这用方案增加了客户类的大小.使用派生则不一样,基类size可以0,所以public继续方案不会增加客户类的大小.
除了上面两种方案,还可以使用private继续,即class Widget: private Counter<Widget>.类似于第一种方案: class Widget: private Counter<Widget> { public: // make howMany public using Counter<Widget>::howMany; ..... // rest of Widget is unchanged };
它直接防止下面的代码: Counter<Widget> *pw = new Widget; //私有继续不答应这样转换
综合看来,public继续方案已经比较完善了.然而,还是有些值得注重的地方.假如有另一个类SpecialWidget,其继续于Widget,对类SpecialWidget的对象计数就只能如下: class SpecialWidget: public Widget, public Counter<SpecialWidget> { public: };
这样,对SpecialWidget的对象计数是正确的,但对Widget对象的计数是错误的.这时Widget的计数是Widget类的所有对象SpecialWidget类的所有对象的总和.为什么 因为每创建一个SpecialWidget对象,Widget构造函数就要调用一次,就增加一次计数.
总结
用模板实现的这个对象计数类可以满足绝大多数需求,但不适用于计数有继续关系的类.本文的核心思想来源于CUG上C++(www.cppentry.com)大师Scott Meyers的一篇文章并有所改动.