//: C05:CountedClass2.cpp // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Erroneous attempt to count objects. #include using namespace std; class Counted { static int count; public: Counted() { ++count; } Counted(const Counted&) { ++count; } ~Counted() { --count; } static int getCount() { return count; } }; int Counted::count = 0; class CountedClass : public Counted {}; class CountedClass2 : public Counted {}; int main() { CountedClass a; cout << CountedClass::getCount() << endl; // 1 CountedClass b; cout << CountedClass::getCount() << endl; // 2 CountedClass2 c; cout << CountedClass2::getCount() << endl; // 3 (Error) } ///:~