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 10706 articles. 11 followers. allnor (8507) is leader. Post Follow

3 Replies
248 Views

Similar Articles

[PageSpeed] 59
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:

special
hello, I would like to use a kind of "plot" that I can't found in Matlab. Maybe one of you know how I could do. I have 3 dimensions of N points for n composants (I want to do a Time/frequency represent) : Frequency (size : nxN), Time (nxN) and magnitude (nxN). These 3 matrix come from the Empirical Mode Decomposition (EMD) of Huang. I think an example should be useful... I want to apply the EMD on a time signal of 5000 points. thus, after the decomposition we have 10 IMF's (for example). My three matrix are : Time : 10x5000 Frequency : 10x5000 Magnitude : 10x5000 (so, ...

Why the special treatment of "high ports"
I've run a few tests with my router/firewall, and found out the following: -if I perform a port-scan, the highest port reported as blocked in firewall's log will be 59316; -if I perform a port-scan using grc.com Shields Up, the ports from 59317 on will be reported "stealth", but only because the software firewall on the computer blocks them (even so, the software firewall's log won't show the consecutive order of blocked ports, but a rather weird sequence) -- otherwise they would appear closed; -ports 65460-65535 are reported as just "closed", not stea...

Currect Path ";.;" needed help please
I've stumbled upon an MCP2 'latest' box which somehow got stripped of its current path pointer in the OS/2 LIBPATH file in CONFIG.SYS here. I'm not sure what update of many provoked that. Adding it to the start of the OS/2 LIBPATH line in CONFIG.SYS restored correct startup of many OS/2 tools, such as WARPIN , PRM, INJOY and, interestingly Seamonkey 1.1.8 PW latest without adding the entry drive\mozilla to that path. Fine .. but in looking over a number of other OS/2 MCP2 boxes I also notice that the ";.;" type entry is also in other path files such as...

Bind RPC Services to a special interface
Hello newsgroup, I am wondering how to prevent RPC services (I don't know if this is exactly the thing I want to do) from binding to all interfaces available in the computer. For example, I don't need a nfs server listening on a dynamic interface like ippp0 (I am using nmap to verfiy which ports are visible). In fact, the services I want to bind to only one interface (eth0) are: a nis server and a nfs server (and perhaps some other portmapped-/tcpwrapped-/rpc-services in the future). I didn't use inetd or xinetd. I had a look on the man pages of tcpd, rpc.mountd and much others bu...

Explicit instantiation and explicit specialization
Hello all, My understanding is that in the 1998 Standard, a template may not be both explicitly instantiated and explicitly specialized for the same type. Did this change in the 2003 Standard and is there anticipated to be any (further) change in the 0X Standard? Thanks, Dave --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ] ...

How much RAM does 5.3c need?
Can I install 5.3c on Indy with 64MB Ram, or do I need 5.3a? Works like a charm on my Indy, but maybe other people have other experiences. E. "Marijan" <address_is_in_post@mail.com> schreef in bericht news:btfqld$gv1$1@bagan.srce.hr... > Can I install 5.3c on Indy with 64MB Ram, or do I need 5.3a? > > ...

How to recognize mouse type
Hi, I am making a setup for few disk-less X terminals and I use a common setup for all of them, but I run on one particular problem: detecting the proper devices attached - especially, the mouse type. Is there a way to detect what kind of mouse is attached to the particular PC (serial, PS/2, USB, other)? From that point I may adapt the X config file (XF86Config) before starting X. I made a config where gpm runs as repeater with two mice - serial and PS/2 and X reads it ourput - works well no matter which mouse is attached, but I would prefer to use the mo...

URPL code need help for speed
Hello, I just wrote this little program to paint grobs in Pict. The algorithm is very simple. You input user coordinates of a pixel inside the box, circle or anything you want to paint and then the program recursively look for 'white' pixels. It works, the only thing is that it is very slow. Any proposal for improving speed? I don't need the code, maybe some suggestion only: %%HP: T(2)A(R)F(.); � 0. 'A' STO C\->PX 1. \->LIST WHILE OBJ\-> DUP REPEAT { # 0d # 0d } PVIEW 1. - \->LIST 'LST' STO B\->R { 1. 0. } { 0. 1. } { -1. 0. } { 0. -1. } 5. R...

FLAIRS 2008: AI Planning and Scheduling Special Track #2
CALL FOR PAPERS - 4 weeks to submission deadline!!! -------------------------------------------------------------------------- AI PLANNING AND SCHEDULING A Special Track at the 21st International FLAIRS Conference (FLAIRS 2008) Coconut Grove, Florida, U.S.A. May 15-17, 2008 http://ktiml.mff.cuni.cz/~bartak/FLAIRS2008 -------------------------------------------------------------------------- The Florida AI Research Society (FLAIRS) hosts the conference in cooperation with the Association for Advancements of Artificial Intelligence (AAAI) since 1988 so FLAIRS is one of the...

Special proxy
Hi, I need to reroute incoming TCP/IP connections for a special protocol, which have passed the Safefire firewall on the way in, out again to another host+port. The firewall only allows this to an internal host. I guess I'm talking about a proxy server, but I only see proxy servers for standard protocols. Are there any that don't care about protocol and port, that is, can I misuse a well-known port for a custom protocol? Mat Nieuwenhoven Mat Nieuwenhoven wrote: > > I need to reroute incoming TCP/IP connections for a special protocol, which > have passed the Safefire fir...

WTB: Run Magazine 1985 & 1986 Special Editions #1 & #2
Hi, Looking to buy the first two special editions of RUN magazine. Special Edition #1 1985 and Special Edition #2 1986. They are marked on the spline. I thought I had a complete set but in sorting through them last night I relized I am missing both of those. I have 3 duplicates issues of RUN if anyone is interested: May 1989, Oct 1989 & Jan 1990. I also have 2 Ahoy duplicates: 1987 Jan & Feb. thanks David Haynes dlh@bombjack.org ...

Need better color control choices for Epson C80
My photos and other color printouts have a bit too much magenta and I'm experiementing with the drive options but the paper and ink costs will put me in the poor house! I'm using matter heavyweight, printing with PSP and have been tinkering with gamma and color management. Does anyone have a good combination that works well? I didn't turn up much definitive on Google. Color management is a difficult issue. It is very possible that your monitor is set at far too warm a temperature and that the printer is reflecting this but your eye does not "see" how off you...

Need some app ideas to sync folders
What are some apps on OS X that will allow me to sync a folder on drive A to a folder on drive B. (Free is always good! ) This is to backup directories with thousands of file and folders so a speedy app that would compare and ad/replace quickly would be much loved. Thanks in advance for any links or suggestions. -- In article <300520121248570833%man@war.ru>, Jason Bourne <man@war.ru> wrote: > What are some apps on OS X that will allow me to sync a folder on drive > A to a folder on drive B. > > (Free is always good! ) > > > T...

SSH Key Types?
Hi, Im trying to get SSH key authentication working between 2 linux boxes. The key formats seems to be different - one (my Mandrake 10 box) seems to be UUEncoded in the form ssh-rsa ABAFSFSDF user@host where the actual key is (i assume) uuencoded the other (a Colbalt Qube( is of the form 1024 35 123456764535(etc) user@host where the actual key part is all numbers. Can anyone tell me what im looking at? ive managed to get it to authenticate from the qube to Mandrake by putting the Qubes format of key in authorized_keys on the mandrake box (i usually use authorized_keys2 for other host...

need sun monitor
hello i looking for a sun monitor has anybody uk side got one they would swop for some othe sun kit? thanks ...

Creat a special matrix?
Hi, Is there someone that can help me with this matrix? I want to create a matrix of this form. 3 7 0 0 0 0 0 0 0 0 0 0 3 7 0 0 0 0 0 0 0 0 0 0 3 7 0 0 0 0 0 0 0 0 0 0 3 7 0 0 0 0 0 0 0 0 0 0 3 7 BL len = 10; A = diag( 3*ones(len,1) ) + diag( 7*ones(len-1,1), 1 ); Michael "BL" <bjartel@math.uio.no> schrieb im Newsbeitrag news:eee70c4.-1@webx.raydaftYaTP... > Hi, > > Is there someone that can help me with this matrix? I want to create > a matrix of this form. > > 3 7 0 0 0 0 0 0 0 0 > 0 0 3 7 0 0 0 0 0 0 > 0 0 0 0 3 7 0 0 0 0 > 0 0 0 0 0 0 3 7 0 ...

Need JAVA Web Developer in Chicago IL
Hi, Hope you are doing great, Kindly go through the requirement and let me know if you have any consultant for that. Senior Java web developer Location: Chicago IL (Need locals) Duration: 6-12 Months Job Description Senior Java web developer is responsible for working on the front end or server side code that is required to build highly scalable, interactive J2EE web applications for Customer Data Management Group. Responsibilities: * Work closely with business and other development teams to design and develop Java/J2EE web applications using broad spectrum of web tech...

continuous YOU NEED TO RESTART YOUR COMPUTER on G3
No one picked this up from (the very tail end of) an earlier thread and I was wondering if anyone knew what to do when this message continues to reappear and I can't get out of it. I started trying to install Panther on my G3 again I had 9.2.2 working with a working CD DVD unit from a junker G4 when I pressed the XPF application to install Panther using ex post facto. In fact I am using the onboard video and the latest version of ExPostFacto. I have a 6 gig hard drive onto which I wanted to install Panther as well as a 10 gig secondary (and a 10 gig Hard drive which came out of the G4 on...

Re: coerce for arbitrary types [3]
vanekl wrote: > Learn to use accumulators: > [More from "Tutorial on Good Lisp Programming Style"] > > (defun product (numbers &optional (key #'identity) > (accum 1)) > "Like (reduce #'* numbers), but bails out early > when a zero is found." > (if (null numbers) > accum > (let ((term (funcall key (first numbers)))) > (if (= term 0) > 0 > (product (rest numbers) key (* accum term)))) Racket: (define (product numbers [key identi...

Data types going to SQL Server
Good afternoon, all; I'm on a new project interacting more closely with SQL Server than I ever had before. I have this problem that I can't seem to solve. I need to write a SAS date value to a SQL Server dateTime field. The numeric variable formatted with date9. is in work.foo and I'm trying to write it to sqlLib.Ed. If I use... Data sqlLib.Ed ; Modify sqlLib.Ed foo( keep=3D CaseId SeqNum LScanDt ) ; By CaseId SeqNum ; Run ; I get datetime values in SQL Server like... 01JAN1960:04:33:00.000 01JAN1960:04:42:00.000 01JAN1960:04:31:00.000 01JAN1960:...

ignore special characters in python regex
Hi, I'm reading text from a file (per line) and I want to do a regex using these lines but I want the regex to ignore any special characters and treat them like normal strings. Is there a regex function that can do this? Here is what I have so far: fp = open('file.txt','r') notes = fp.readlines() fp.close() strin = "this is what I want" for note in notes: if re.search(r""+ str(note) + "",strin): print "Found " + str(note) + " in " + strin Thanks for any help Astan Chee wrote: > I'm reading text fr...

Need H1B Consultant For GEMSCorp
Hi, If you have an aspiration to work in USA and thought the only way to do that was H1 then this is the mail will greatly help you where you can fulfill your dream!!!!!! We file H1 visa for IT candidates having minimum two and half years of live experience. Please let me know if you have suitable matching for the below mentioned skills who are willing to apply for H1 B visa. Skills set: Primarily SAP, PeopleSoft, Java developer/architect, Oracle developers, ASP.Net, Siebel, Tibco, Mainframes, and Testing. GEMS Inc. is a Software Consulting, Project Management & Global Technology Servi...

SAS Analyst Needed in Phoenix
I am recruiting for a SAS Analyst in Scottsdale, AZ. Responsibilities include: =B7 Support and lead in corporate consulting engagements, which requires competencies in financial modeling (using SAS) and accounting, as well as the ability to develop a proficiency in our valuation techniques which are mathematically-based =B7 Support in product development, which requires a mastery of accounting and finance principles, as well as a proficiency in IT systems for the purposes of interfacing with the development team (as our products are web-based tools) =B7 Support in R&D, pursuant to our co...

FILE-TYPES library
Greetings all, I've just released FILE-TYPES, a library to classify types of files based on PATHNAME-TYPE. It also supports MIME types. Funny enough its the first serious CL code I ever wrote. I still find it very useful though, and think the design is good. For information see: http://mr.gy/software/file-types/ http://mr.gy/software/file-types/manual.html http://mr.gy/software/file-types/api.html As far as I can tell, the CL community has no library to identify types of files, a task that comes up commonly. While this library doesn't do any fancy magic number checks ...

All the binding attractions in all the invisible forces must be seen as PHASE COHERENCE so type "phase coherence" into Google.
All the binding attractions in all the invisible forces must be seen as PHASE COHERENCE so type "phase coherence" into Google. Then you will find - - what Einstein couldn't - - the fact that I stated about "phase coherence". It's at many places in Google. It was at the #1 TOP spot, in Google, from Sept./22/2010 . . . to . . . Oct./04/2010 (Fitz Finds a fact! - - Phase Coherence is the reason...) Why did it take top spot in Google? Because it's REALLY what is happening. To see it click link: http://www.amperefitz.com/top.spot.htm Daniel P...