I know similar questions have been answered before but I have searched stackoverflow (et al) and haven't found a clear idea on what to do with a small class that is instantiated and used only once in the program. Is it really important to still have the declaration and implementation in separate files?
Take the following example:
// timer.hpp
#pragma once
#include "presets.h" // includes #defines for u64 -> uint64_t, etc
class Timer {
public:
Timer() {}
Timer(u64 elt) : elt_(elt) {}
void startTiming() { if (NOT running_){ running_ = true; sTime_ = GetTickCount64(); }};
void stopTiming() { if (running_) { running_ = false; eTime_ = GetTickCount64(); elt_ += (eTime_ - sTime_); sTime_ = eTime_; }}
u64 getElapsed() { if (NOT running_) return elt_; eTime_ = GetTickCount64(); return elt_ + eTime_ - sTime_; }
private:
bool running_ = true;
u64 elt_ = 0, eTime_ = 0, sTime_ = GetTickCount64();
};
Everything I have read insists that declaration and implementation be in separate files but it seems absurd to split up such a simple class into .h file and .cpp file. It is very rarely ever changed and instantiated only once.
I have some other classes, a little bigger and also used only once that I currently have in 2 files. Assuming a class is only instantiated once in the program, my questions are:
- Is it reasonable to put declaration and implementation of a small class in a single file? If not, why not?
- How large does a class need to be before it's better to separate into 2 files?
I know there are very similar questions already existent here but I haven't read any that gave a clear answer to the above.