|
|
Special type of singleton needed
I need to design a class that meets the following requirements:
-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;
Any ideas and comments are welcom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
BigMan (211)
|
2/15/2005 9:55:43 PM |
|
BigMan wrote:
> I need to design a class that meets the following requirements:
>
> -the class may not have more that one instance;
> -the instance needs arguments for initialization;
> -accessing the instance must not require any arguments;
>
> Any ideas and comments are welcom
>
>
> [ See http://www.gotw.ca/resources/clcm.htm for info about ]
> [ comp.lang.c++.moderated. First time posters: Do this! ]
I hope you are looking for some program like this:
#include <stdio.h>
#include <string.h>
class CSingleton
{
public:
static CSingleton* CreateInstance(char* chName = NULL);
char* GetName() { return m_chName; }
static CSingleton* GetInstance() { return m_pInstance; }
#define MAX_NAME_LEN 256
protected:
CSingleton();
~CSingleton();
private:
static CSingleton* m_pInstance;
static char m_chName[MAX_NAME_LEN];
};
char CSingleton :: m_chName[MAX_NAME_LEN];
CSingleton* CSingleton :: m_pInstance = NULL;
CSingleton :: CSingleton()
{
strcpy(m_chName, "");
}
CSingleton :: ~CSingleton()
{
}
CSingleton* CSingleton :: CreateInstance(char* chName)
{
if(m_pInstance == NULL)
{
m_pInstance = new CSingleton();
strncpy(m_chName, chName, MAX_NAME_LEN);
}
return m_pInstance;
}
int main()
{
CSingleton* pSingleton1 = NULL, *pSingleton2 = NULL, *pSingleton3 =
NULL;
pSingleton1 = CSingleton::CreateInstance("SingletonTest1");
pSingleton2 = CSingleton::CreateInstance("SingletonTest2");
pSingleton3 = CSingleton::GetInstance();
printf("\npSingleton1 Name = %s", pSingleton1->GetName());
printf("\npSingleton2 Name = %s", pSingleton2->GetName());
printf("\npSingleton3 Name = %s", pSingleton3->GetName());
return 0;
}
Regards,
Bhanu Gogineni.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
bhanu
|
2/15/2005 11:46:42 PM
|
|
BigMan wrote:
> I need to design a class that meets the following requirements:
>
> -the class may not have more that one instance;
> -the instance needs arguments for initialization;
> -accessing the instance must not require any arguments;
What's wrong with the standard approach: explicitly initialize it in the
first lines of main function or even earlier before any access to the
instance occurs?
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Maxim
|
2/16/2005 9:34:49 PM
|
|
BigMan wrote:
> I need to design a class that meets the following requirements:
> -the class may not have more that one instance;
> -the instance needs arguments for initialization;
> -accessing the instance must not require any arguments;
I usually use a variant of the singleton pattern, with an
additional static function initialize; instance asserts that
initialize has been called, rather than creating the instance.
--
James Kanze GABI Software
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
kanze
|
2/16/2005 10:40:06 PM
|
|
|
3 Replies
123 Views
(page loaded in 0.225 seconds)
Similiar Articles: Trying to import multiple images into MATLAB and treat them as ...Hello.I'm in desperate need of some help here, I want ... Please can you offer me some type of solution. The main ... to squeeze() that to get rid of the leading singleton ... Solaris SunOS 5.9 Generic Terminal types - comp.unix.solaris ...Is toe still the command to get a list of the available terminal types? Anything special to do to get it to work from a basic user login? Or do we need a different command? special character ' - comp.databases.mysql> > which is the correct way to handle this type of situations ... comp.lang.python I am looking for a list of special character in python regular expressions that need to be ... Help needed: read 3-dimensional array from a MAT-file in Fortran ...... fpDeallocate(fp) mxDestroyArray(mx) Take special note ... interface then passing the variable data would not need ... to run it under another version, indicating some type ... Sign a document with a pen (tablet pc) - comp.text.pdfThere is a special type for a signature, but it seems I can't realise it with them, since so far just the possiblilty to write in that field is needed, no real signature ... Find Color/Pixel in RGB BMP - comp.soft-sys.matlabHi, i want to find all pixels which have a special color (e ... in pdf - comp.text.pdf Parse the header to find the type of BMP (24-bit, 8 ... bit, you now know you need an ... Hatching for Patent Drawings - Adding New Types of Hatching - comp ...So if you need a special hatching, an ACAD file might work. However, I havent been able ... are also the same except for embedding compiled shape files in the > line type ... variant data type - comp.lang.fortran... null value to a variable that is not a variant data type" about 80% of the time. I really need ... The SQL Server 2000 sql_variant data type is a special data type that ... Detecting font size change - comp.lang.javascriptI'm looking for an event type of notification somehow because I need to make some modifications based ... special">some text goes here</div></td> with CSS div.special ... Signature capture on form? - comp.text.pdfEven if they need to annotate a PDF all isn't lost. You could use a PHP ... ... Sign a document with a pen (tablet pc) - comp.text.pdf There is a special type for a ... When is a Singleton not a Singleton?... class names or Class objects to Singleton instances. Again, the runtime type ... later to be reloaded when the Singleton is again needed. ... classes (perhaps through special class ... Singleton pattern - Wikipedia, the free encyclopediaSince Java 5.0, the easiest way to create a Singleton is the enum type ... If the program will always need an instance, or ... solution is thread-safe without requiring special ... 7/23/2012 7:06:38 PM
|
|
|
|
|
|
|
|
|