I have the following code and I wanted to create a class that encapsulates the timer but the trouble I’m having is using the timer after declaring the class. I posted another block of code to show an example of how I want to use the timer.
double diffclock(clock_t clock1,clock_t clock2)
{
double diffticks=clock1-clock2;
double diffms=(diffticks*1000)/CLOCKS_PER_SEC;
return diffms;
}
void some_function()
{
clock_t begin = clock();
//do something
clock_t end=clock();
cout << "Time elapsed: " << double(diffclock(end,begin)) << " ms"<< endl;
}
this is the header file
#ifndef SPECIALCLOCK_H
#define SPECIALCLOCK_H
#include <ctime>
class specialclock
{
private:
public:
specialclock(){}
~specialclock(){}
double diffclock(clock_t clock1,clock_t clock2);
};
double specialclock::diffclock(clock_t clock1,clock_t clock2)
{
double diffticks=clock1-clock2;
double diffms=(diffticks*1000)/CLOCKS_PER_SEC;
return diffms;
}
#endif
And this the the main file
#include "specialclock.h"
int main()
{
specialclock timer;
//how would I use the timer here?
return 0;
}
The way you have your class set up doesn’t really make sense. I have a simple timer class that works like this:
Is this what you’re looking for? If so, my class is set up like the following:
This is simplified for the sake of clarity. You can see my full source here and here.