Special type of singleton needed

  • Follow


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:













7/23/2012 7:06:38 PM


Reply: