f



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
BigMan (215)
2/15/2005 9:55:43 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8506) is leader. Post Follow

3 Replies
362 Views

Similar Articles

[PageSpeed] 37

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
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
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
kanze
2/16/2005 10:40:06 PM
Reply:

Similar Artilces:

C API needs array of C strings, what c++ type to hold it?
WWSAPI represents an array of strings as (WCHAR **array, int count). I need to allocate temporary storage for the array, and for the converted strings. Like this: array = new WCHAR *[count]; for(int i = 0; i < n; ++i) array[i] = widen(get_input(i)); What data structure should I use? I can use std::vector for the array, but what do i put in it? Note that it is passed to C, so the memory layout has to be the same. I can use the subset of C++11 supported by Visual 2010, and boost. -- Fredrik Stax\"ang | rot13: sfgk@hcqngr.hh.fr This ...

Is there anything special I need to do in order to mix object files created by the C and the C++ compilers?
I created one *.o file from a C source, using gcc I created another *.o file from a C++ source, using g++. The linking pass is failing. Should I use gcc, g++ or ld for linking? I am guessing the latter. Are there any special flags, steps or gotchas? TIA, -Ramon On Sep 10, 7:49=A0pm, Ramon F Herrera <ra...@conexus.net> wrote: > I created one *.o file from a C source, using gcc > > I created another *.o file from a C++ source, using g++. > > The linking pass is failing. > > Should I use gcc, g++ or ld for linking? I am guessing the latter. > > Are there ...

mapping of XML types to C/C++ built-in types
Hello, Does W3C defines a standard for mapping XML types to C/C++ built-in types and vise versa? Thank you, Alona Hi Alona, "Alona" <allab@sympatico.ca> writes: > Does W3C defines a standard for mapping XML types to C/C++ built-in > types and vise versa? I don't think there is such a thing as XML type. Schema languages (e.g., W3C XML Schema) normally define primitive types. Some of them can be mapped to C/C++ fundamental types but I don't think W3C defines such a mapping. One proprietary mapping of XML Schema built-in types to C++ types is described in this...

!!! need to write an agent in C++ or C
Hi All, I'm newbie in SNMP. I NEED to write snmp agent in c++ or c for solaris. Is there a free agent toolkit available on SUN.com site? I know that there is a JDMK available. Is there any other toolkit (on SUN.com)? If no, can I use JDMK to develop an agent in c++ or c language? I'm not interested in AdventNet toolkit. BTW: is this good toolkit? Help me please, Thanks. Regards P miniu wrote: > I NEED to write snmp agent in c++ or c for solaris. Do you need to write one from scratch, or can you build on existing software? You might like to have a l...

Needed Sr. C / C++ / C# developer !
A leading financial institution is seeking a C / C++ / C#/.Net software developer with 5 to 6 years of programming experience to develop, test and support new and existing mission-critical global trading applications. mailto:prominc@yahoo.ca ...

Needed Sr. C / C++ / C# developer !
A leading financial institution is seeking a C / C++ / C#/.Net software developer with 5 to 6 years of programming experience to develop, test and support new and existing mission-critical global trading applications. mailto:prominc@yahoo.ca ...

type ranges for c,c++,java,c#
hi, is all the type range (int,float,double,...) for all languages same? Does anyone has URL of the type range for different languages? thank you On Tue, 30 Sep 2003 13:14:23 +0800, "smith flyers" <flyer@masfd.com> wrote: >hi, is all the type range (int,float,double,...) for all languages same? No. For C and C++ only minimum and relative ranges are defined for atomic types. However, C99 has a header that optionally defines exact size types. For Java and C# the ranges are exactly defined, and I believe they are the same for corresponding types. C# has unsigned inte...

Needed Sr. C / C++ / C# developer !
A leading financial institution is seeking a C / C++ / C#/.Net software developer with 5 to 6 years of programming experience to develop, test and support new and existing mission-critical global trading applications. mailto:prominc@yahoo.ca ...

Needed Sr. C / C++ / C# developer !
A leading financial institution is seeking a C / C++ / C#/.Net software developer with 5 to 6 years of programming experience to develop, test and support new and existing mission-critical global trading applications. mailto:prominc@yahoo.ca ...

Needed Sr. C / C++ / C# developer !
A leading financial institution is seeking a C / C++ / C#/.Net software developer with 5 to 6 years of programming experience to develop, test and support new and existing mission-critical global trading applications. mailto:prominc@yahoo.ca ...

Need asm ARM ,GP2X box , dont need C/C++
Need to asm short piece of code , but i dont need the problems of compiling somethin in C/C+ ..... Modern h/w would allow it from the keys , as a pocket calculator does ..... I need to use this tiny code to bootstrap larger OpSys .. Its not a port , its a new clever OpSys to replace Linux . It has no compiling , interpreting , any upgrade is done in real time in seconds . Then you simply write it back to HDD/SD card and switch the machine off . The Hdd drive saved that small snip , adds it back in and boots the old OpSys if the new part crashes the machine . I...

Help needed with a C++ syntax (legal in C, but not C++)
Greetings, I'm trying to use a vender-supplied API in my C++ program. The vender's API is in C and it contains something similar to: type int INT; struct S { int i; double INT; /* OK in C, not C++ */ } According to a C/C++ book (C, A reference manual by S Harbison and G. Steele JR), the above struct is actually legal in C, as typedef names can be redeclared as objects in inner scopes. However, in C++ it is not permitted to do so within a structure or union -- which are scopes -- if the original typedef name has been used in the structure or union already. The problem I'm f...

The need of Unicode types in C++0x
Hi, I am currently learning QT, a portable C++ framework which comes with both a commercial and GPL license, and which provides conversion operations to its various types to/from standard C++ types. For example its QString type provides a toWString() that returns a std::wstring with its Unicode contents. So, since wstring supports the largest character set, why do we need explicit Unicode types in C++? I think what is needed is a "unicode" locale or at the most, some unicode locales. I don't consider being compatible with C99 as an excuse. Correction: Ioannis Vranos wrote...

Web resources about - Special type of singleton needed - comp.lang.c++.moderated

John Singleton Copley - Wikipedia, the free encyclopedia
... American-born painter, active in both colonial America and England. He was probably born in Boston , Massachusetts , to Richard and Mary Singleton ...

Chris Singleton (@c_singleton) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Andy Singleton - LinkedIn
View Andy Singleton's professional profile on LinkedIn. LinkedIn is the world's largest business network, helping professionals like Andy Singleton ...

boston copley square john singleton copley bruins jersey back view - Flickr - Photo Sharing!
Explore photographynatalia's photos on Flickr. photographynatalia has uploaded 10879 photos to Flickr.

L.T. Hutton: John Singleton Didn't Quit 2Pac Movie, He Was Fired - YouTube
http://www.vladtv.com - During an interview with The Goin Way Back Show, executive producer of the upcoming 2Pac biopic L.T. Hutton addressed ...

John Singleton eyes Prime Media after board departure
Macquarie Radio Network director John Singleton has expressed interest in the regional TV company Prime Media, after Prime foreshadowed the departure ...

John Singleton cautioned by Gina Rinehart's son John Hancock
... of her most persistent legal protagonists John Hancock has some sage advice for his mother's defenders, namely her long-time friend John Singleton. ...

John Singleton retires More Joyous
JOHN Singleton is preferring to take the odds on an equine match made in heaven rather than risk the reputation of champion More Joyous with ...

John Singleton's holiday home on the Central Coast goes up in flames
IRREPLACEABLE paintings by artist Bill Leak have been destroyed in a suspicious fire that ripped through John Singleton's holiday home.

John Singleton and Gai Waterhouse's day of reckoning arrives
GAI Waterhouse, this morning, might be wondering why she didn't toss Singo in the deep end of his Rose Bay pool that legendary day they first ...

Resources last updated: 2/11/2016 12:16:32 PM