Consider the following code parts:
this is Timing.h:
class Timing {
public:
//creates a single instance of timing. A sinlgeton design pattern
Timing *CreateInstance();
private:
/** private constructor and a static instance of Timing for a singleton pattern. */
Timing();
static Timing *_singleInstance;
};
extern Timing timing;
and this is Timing.cpp:
Timing timing; //a global variable of Timing
Timing *Timing::CreateInstance() {
if (!_singleInstance) {
_singleInstance = new Timing();
}
return _singleInstance;
}
Now, since I want to hold one object of Timing i made as a singleton pattern (only one timing can be created). In my exercise requirements, they say I can choose between 2 option:
create one instance of timing in the main() which is in another file and each time pass a reference of this instance to the rest of the methods in the program, or I can create a global Timing timing and state in .h file extern Timing timing. I chose the second option. However, i have some difficulties to connect between the global variable and my singleton pattern.
how to i create the instance of timing in the Timing.cpp file?
i tried Timing timing = CreateInstance(), however this doesn't work..
i can't create the instance in the main() because then i will be implementing the first option..
do i need to write in main timing.CreateInstance() ?
i hope i explained my self clearly.
thanks for your help