Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
while creating the singelton design pattern, how to restrict to create more than one object.
i have below code.
#include<iostream.h>
class Singelton
{
    // Private Constructor
    Singelton()
    {
    }
 
    static Singelton *s_instance_p;
 
public:
~Singelton()
{
 

}
    static Singelton* get_instance();
    void method();
    static void DestroyInstance();
};
Singelton* Singelton::get_instance()
{
 
   return s_instance_p;
 
}
void Singelton::method()
{
    cout<<"Method function called"<<endl;
}
void Singelton::DestroyInstance()
{
    delete s_instance_p;
    s_instance_p = 0;
}
Singelton* Singelton::s_instance_p = NULL;
 
void main()
{
Singelton *ssl_ctu_ptr = Singelton::get_instance();
ssl_ctu_ptr->method();
Singelton *ssl_ctu_ptr1 = Singelton::get_instance();
ssl_ctu_ptr1->method();
//delete (ssl_ctu_ptr);//never call delete on the singleton pointer in main()
Singelton::DestroyInstance();
}
 
here i have created two instances ssl_ctu_ptr , ssl_ctu_ptr1.
Both are working fine. i am unable to understand how to restrict more than 1 instances.
please provide your solution
Thanks in Advance.
Posted 3-Feb-13 1:48am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Instead of having a flag we could also check whether the static instance is null and then create the singleton instance.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Firstly, you have a spelling error... it is "Singleton".
 
Solution 2 is correct that you should check the static instance pointer and not a flag.
 
Neither of these choices is threadsafe. If you have more than one thread, you need to protect the first instance creation with a critical section.
 
Since the stated language desired is C++, you can define the static instance in the get_instance() method:
 
Singelton* Singelton::get_instance()
{
    static Singelton* pThis;
    if(!pThis)
    {
        pThis = new Singelton();
    }
    return s_instance_p;
}
 
If the app is multithreaded, the 'if' statement needs to have critical section backup.
 
You can't use this technique in java or several other languages.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
You need to restrict in get_instance() method. Please find the below modified code.
#include <iostream>
#include <conio.h>
 using namespace std;
 class Singelton
{
    // Private Constructor
    Singelton()
    {
    }
	static bool instanceFlag; //added
    static Singelton *s_instance_p;
 
public:
	~Singelton()
	{
 
 
	}
    static Singelton* get_instance();
    void method();
    static void DestroyInstance();
};
 
 bool Singelton::instanceFlag = false;
Singelton* Singelton::s_instance_p = NULL;
 
Singelton* Singelton::get_instance()
{
 
	if(! instanceFlag)
    {
        s_instance_p = new Singelton();
        instanceFlag = true;
        return s_instance_p;
    }
    else
    {
        return s_instance_p;
    }
 
}
void Singelton::method()
{
    cout<<"Method function called"<<endl;
}
void Singelton::DestroyInstance()
{
    delete s_instance_p;
    s_instance_p = 0;
}
 
int main()
{
	Singelton *ssl_ctu_ptr = Singelton::get_instance();
	ssl_ctu_ptr->method();
	
	Singelton *ssl_ctu_ptr1 = Singelton::get_instance();
	ssl_ctu_ptr1->method();
	
	//delete (ssl_ctu_ptr);//never call delete on the singleton pointer in main()
	Singelton::DestroyInstance();
 
	getch();
	return 0;
}
</conio.h></iostream>
 
In get_instance() method, we need to check whether the object is already created or not. if it is created then we need to return the object instead of creating new.
 
Hope this makes you understand!
 
Best Regards
Muthuraja
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141002.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100