????????????????????????C++?????????????????????????Щ???????????C++????
??????????????????????????????????????????д????ж???????????????????????????t??????????????£?
????01.classWidget {
????02.public:
????03.Widget() { ++count; }
????04.Widget(constWidget&) { ++count; }
????05.~Widget() { --count; }
????06.
????07.staticsize_t howMany()
????08.{return count; }
????09.
????10.private:
????11.staticsize_t count;
????12.};
????13.
????14.//cpp?????
????15.size_tWidget::count = 0;
????????????????????????????????????????
??????????????????ж??????????????????????????м??????????δ??????????????????£?????????????ü?????????t????????
?????????????κ?????????????????????????????????????????
??????Ч??????????????С??????????????????
?????????????????????????????á?
???????????????????????????????????
01.classCounter {
02.public:
03.Counter() { ++count; }
04.Counter(constCounter&) { ++count; }
05.~Counter() { --count; }
06.staticsize_t howMany()
07.{return count; }
08.
09.private:
10.staticsize_t count;
11.};
12.
13.// This still goes in an implementation file
14.size_tCounter::count = 0;
???????????Counter????????????????????磺Widget???????????????????????
01.// embed a Counter to count objects
02.classWidget {
03.public:
04......// all the usual public
05.// Widget stuff
06.staticsize_t howMany()
07.{return Counter::howMany(); }
08.private:
09......// all the usual private
10.// Widget stuff
11.Counter c;
12.};
13.
14.//or:
15.
16.// inherit from Counter to count objects
17.classWidget: public Counter {
18......// all the usual public
19.// Widget stuff
20.private:
21......// all the usual private
22.// Widget stuff
23.};