Code:
unsigned long count_it()
{
static unsigned long count = 0;
++count;
cout << "The number of times this function has been called is " << count << "\n";
return count;
}
Unsigned int is a lot nicer for counters than a signed int.
Returning the count is a nice convenience feature. I would actually make a class to wrap the counting and allow access to count.
For example:
Header:
Code:
class CallCounter
{
public:
static unsigned int increment() { return ++m_count; }
static unsigned int getCount() const { return m_count; }
static void reset() { m_count = 0; }
private:
static unsigned int m_count;
};
Source:
Code:
unsigned int CallCounter::m_count(0);
Usage:
Code:
void myFunc1()
{
CallCounter::increment();
}
void myFunc2()
{
std::cout << "Called " << CallCounter::increment() << "times." << std::endl;
}
void myFuncReport()
{
std::cout << "Total called " << CallCounter::getCount() << "times." << std::endl;
}
int main()
{
myFunc1();
myFunc2();
//...
myFuncReport();
return 1;
}