Reputation: 4793
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
Upvotes: 2
Views: 1689
Reputation: 60341
You don't need to create the instance.
Change the name of CreateInstance() to GetInstance().
Then, when you want to access your timing object, call GetInstance. The singleton will be created the first time you access it.
You won't be implementing the first option if you do this because you won't keep hold of the return value from GetInstance in a variable. You'll just use it straight away.
Don't do first option and keep hold of t:
Timing* t = Timing::GetInstance();
t->DoSomething();
Do the following when you want to use your singleton.
Timing::GetInstance()->DoSomething();
(That's the basic guts of the answer anyway - technically, you can hold on to t for a while, and as long as you don't keep passing it as parameters into every function that want it, you're doing option 2)
That'll be sufficient for the task you're doing.
In production code, life times of singleton often need better control. To do that, I'd recommend having three functions:
Timing::CreateInstance()
Timing::GetInstance()
Timing::DestroyInstance()
Call Create and Destroy from the start and end of your main function. But, BE AWARE, you need to be careful that no code (even that in destructors) will access GetInstance() after you have called DestroyInstance(). I'd recommend you at least put an assert in the GetInstance function to:
assert(_singleInstance != 0)
return _singleInstance;
And, in DestroyInstance do:
delete _singleInstance;
_singleInstance = 0;
Upvotes: 2
Reputation: 32831
CreateInstance should be static, and you would do something like:
Timing *timing = Timing::CreateInstance();
When you need the single instance, and you would not need the timing global variable (only the static variable _singleInstance).
Oh, and as pointed out by Scott Langham, GetInstance would be a better name than CreateInstance.
Upvotes: 2