Meyers singleton--Why cant we create two objects?
1 2 3 4
|
Singleton& Singleton::getInstance() {
static Singleton instance;
return instance;
}
|
When We define getInstance() method like above...then
1 2 3 4 5 6
|
int main()
{
singleton*s1=singleton::getInstance();
singleton*s2=singleton::getInstance();
return 0;
}
|
when we execute above code .doesnt
getInstance()
method execute
static singleton instance
line twice and create two different instances?
getInstance() is called twice, but both calls return the address of the same object.
Why cant we create two objects? |
Singleton is a "pattern" that describes that effect. The code is just an implementation of that pattern.
https://en.wikipedia.org/wiki/Singleton_pattern
Last edited on
Topic archived. No new replies allowed.