COMPGROUPS.NET | Post | Groups | Users | Stream | Browse | About | |

### Special type of singleton needed

• Email
• 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 (215) 2/15/2005 9:55:43 PM

See related articles to this posting

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

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

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
 comp.lang.c++.moderated 10692 articles. 11 followers.

3 Replies
238 Views

Similar Articles

[PageSpeed] 29

• Email
• Follow

Similar Artilces:

NEED HELP: convert pfm (PC Type 1) or .otf (Open Type Font) to .ttf (True Type)
Hello, I tried many times to convert a font (called Charter) to a true type font in windows xp. I have two font formats available: .pfm (PC Type 1) and .otf (Open Type Font). To convert, I tried to use Adobe Type Manager and Crossfonts - both cannot convert the two available font formats into True Type. I am in dispair - can anyone help? Any solution is welcome! Chris PS: The reason why I need this font in True Type is a : It seems that I need a true type font when I use SMARTDRAW. SMARTDRAW only offers true type fonts when I want to type text, and SD seems to ignore other fonts tha...

Static Typing Where Possible and Dynamic Typing When Needed
Hi all, There is the new article http://lambda-the-ultimate.org/node/view/834 about the Static Typing and Dynamic Typing in Dynamic Languages Best Regards, Vladare ...

Template specialization with non-type parameters of different types
Hello, I want to have a template that has a non-type parameter, whose type would vary among instantiations/specialisations. Particularly, I want it to take pointers to functions of different types. The code is template <class T, T t> class Foo; template <class Ret, class Arg, Ret (*t)(Arg)> class Foo<Ret (*)(Arg), t> { public: void f(); }; template <class Ret, Ret(*t)()> class Foo<Ret (*)(), t> { public: void g(); }; int f1(); int f2(int); int main() { Foo<int(*)(), &f1>().g(); Foo<int(*)(int), &f2>().f(); } However, this mea...

need create RTF-type file and DOC-type file
i need library to create RTF-type file and DOC-type file. can be two differt library for RTF and DOC -- Posted via http://www.ruby-forum.com/. huh? Have a look at Abiword (http://www.abisource.com/). It can be run from command line to do document conversion. ...

Explicit specialization of member template when type is another template type.
Here is a program, it looks weird but it models what I'm trying to do. In this program: - "Object" is just some example type. - "Container" holds a single item of some type, but provides access access to a copy of that object only via a nested class Container::ContainedItem. === BEGIN === // An example object with a member function. class Object { public: void function () const { } }; template <typename T> class Container { private: T data_; // Container's instance of the data. public: // This just wraps an item. class ContainedItem { private: T...

do I need Singleton?
I'm a newbie at Corba/Ace/Tao. So, this may be a real silly question, but here goes... I want to create a number of Manager objects. Typically, if I were developing using Java, I probably would typically create a manager object using the Singleton pattern. The manager would maintain a static reference, and return a reference to that instance via a get method. Since Corba's has a "naming service", does the Singleton implementation apply to the manager object described above ? ...

Need ideas -> generic storage of type registration with unknown type
I'm using the boost::serialization library for the IO for my objects. I'd like to come up with a way to register objects with the archive in a generic manner something like so: struct type_registrar { template < typename T > static bool register_type() { singleton()->register_functions.push_back(boost::bind (&Archive::register_type<T>, _1)); } static type_registrar * singleton() { static type_registrar * singleton_ = new type_registrar; return singleton_; } static void register_all(Archive & ar) { BOOST_FOREACH(boost::funct...

What is the purpose of type() and the types module and what is a type?
The type() builtin according to python docs, returns a "type object". http://docs.python.org/2/library/types.html And in this module is bunch of what I assume are "type objects". Is this correct? http://docs.python.org/2/library/functions.html#type And type(), aside from being used in as an alternative to a class statement to create a new type, really just returns the object class, doesn't it? >>> import types >>> a = type(1) >>> b = (1).__class__ >>> c = int >>> d = types.IntType >>> a is b is c is...

Who needs types if we have terms?
Or, in other words, is a term a data type?. They are usually used like data types? Because I've read a lot of post about prolog typed/untyped, but I'm wondering me if the only diference is in type (term) checking. "tmp123" <tmp123@menta.net> writes: > Or, in other words, is a term a data type?. They are usually used like > data types? > Because I've read a lot of post about prolog typed/untyped, but I'm > wondering me if the only diference is in type (term) checking. "Data type" can mean a lot of things, and you could describe the set...

Why does delay need to be special?
hi Why does delay need to be special? Let's say my interpreter is simply the eager kind. (the simple MC-eval in sicp 4.1) (pp stream-enumerate-interval) (named-lambda (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons low (lambda () (stream-enumerate-interval (+ low 1) high))))) Let's say I'm not lazy and like to hand translate all my stream related functions by hand into lambdas. Do I need delay at all? Does the interpreter has to support it somehow? I can't figured out what's the point of having a special syntax for delay. (I ...

a special type of function
Hi, I want to write function that is of type: void Funt(long az, double bt, long *ar1, double *ar2) I got the function to work for the arrays: ar1 and ar2 but I don't know how to get it to work for variables: az and bt. Can anyone show me if and how this is possible. Thanks, /Sheldon On Nov 14, 1:15=A0pm, Sheldon <shejo...@gmail.com> wrote: > Hi, > > I want to write function that is of type: void Funt(long az, double > bt, long *ar1, double *ar2) > I got the function to work for the arrays: ar1 and ar2 but I don't > know how to get it to work for variables: ...

help with types needed
Hi, I am trying to create a specific structure, but having problems with the type definitions. Basically it's an array of trees. Each element of the array should be a binary tree of type TREE. TREE is a pointer to a node TREENODE of the tree which containers pointers to the children. typedef struct TREENODE *TREE; struct TREENODE { USERID uid; TREE leftChild, rightChild; }; /* Array of Tree pointers */ typedef struct TREE USERBASE[100]; The array is important since the index provides infomation about the what nodes are in the tree. The tree definition looks and seems to work fine,...

need some special key
hi i got a table with a lot of datasets, one row is integer. what i need is that only one dataset can have a '1' in this row, every other dataset has to be '0'. sometimes the '1' can move to other datasets, but there has alwas to be only one '1' in the whole row. is it possible to create something like a very special key over this row? thanks for help replay wrote: > hi > > i got a table with a lot of datasets, one row is integer. what i need > is that only one dataset can have a '1' in this row, every other > dataset has to be '0...

When you need an integral type. . .
When you simply want to store a number, what integral type do you use? For instance, let's say we have the following in a Poker game: struct Card { enum Suit { Hearts, Diamonds, Spades, Clubs } suit; int value; }; Looking at the "value" variable above, it's range needs to be 0 to 51. How many people would use an "int"? How many people would use an "unsigned char"? Upon first thought, I would presume that the most appropriate choice would be "unsigned char", as the number shall always be positive, and it has sufficient range. I...

a special type of strings!
Hi Everyone, I have an alphabet A= {a,b,c,d,e,f,g,h}. letters in A are grouped as follows: group1: G1={a} group2:G2={b,c}, group3:G3={d,e,f}, group4: G4={g,h}. In other words, we have {{a},{b,c},{d,e,f},{g,h}} as an ordered partition of A. We say the string \alpha=s_1,s_2,s_3,...,s_n to be NORMAL if and only if for every i,j: ( if s_i is in Gj then, (s_(i+1) is in Gj or s_(i+1) is in G(j-1) ) OR (s_i is in G1). For instance the following three sequences are normal: Seq1: d,b,a,g,e,f,c,a,e Seq2: a,d,b,a,b,e,c,a,h. Seq3: g,g,h,d,f,d,b,c. I need to wri...

Special needs GS?
I just acquired a GS from a junk store. It was from a school, and is a ROM 1. It had a memory card, but only with the stock 256k RAM in it. It also was fitted out with an Adaptive Peripherals card that has two cables that go to an external box with a Centronics type connector, a switch, and two mini-plug sockets that say SW-1 and SW-2. The card has a 65SC816 processor on it, a ROM, but no RAM that I can see. There is also an Echo sound card in there. I assume there was some program that needed that sound card and did not work with the standard GS sound? There was also a Franklin card. It ha...

need a special distro
I have been scanning the hundreds (thousands?) of Linux distros but haven't found what I need for a special app. 1. It should run from a CDR or even floppies without requiring hard disk space. 2. It should operate successfully on a computer with just 64MB of main memory and an old monitor. 3. It should have lynx built into the package. Why do I need all this? Well I have an old Windows based computer linked to my more modern Slackware 9.1 box. I want to leave the Win 98SE installation intact. I can ping back and forth and even ftp from one to another, using the second cdr of the Slack ...

Special Switch needed
I need a switch that takes a samples from port 1, b samples from port b and so on. I?ve found a manual switch, a switch that is controlled by a threshold, the multiport or index switch (Both are the same), a n-sample switch and a n-sample enable switch. The difference between the n-sample switch and the n-sample enable switch is that the first one switches after n- samples two a second port and the other one switches to ground. So you can easily substitute the n-sample enable switch by a n-sample switch and a ground block. Both can be reset but both don?t accept a vector as ...

typing special characters
How can I type special characters directly from the keyboard? On Tuesday 16 Sep 2003 07:20 in <pan.2003.09.16.06.20.04.406762@iastate.edu>, Mohamad Al-Saqer (saqer@iastate.edu) wrote: > How can I type special characters directly from the keyboard? Do a Google search on this newsgroup. I asked that same question a few months ago and received several different answers, all of which were correct. [There's more than one way to skin a cat.] -- Regards, Dave [RLU#314465] ====================================================== dwnoon@spamtrap.ntlworld.com (David W Noon) Remove spa...

Need help with singleton!
Hi :) Yea, again a question to help me building the game XD I have a class called Mob, when i instance a mob from the main i search the name of mob in a list, if the name is there i create it and i call a singleton class with all the images of the mob, so if i create the same kind of mob again he will create it and use the images from the singleton, to save memory. Well here comes the problem, if all mobs die and i go town and i never see this mob again, the singleton gonna still in the memory? 1 mob isnt trouble, just 2mb, but if i see 200 kinds of mob its alot of memory and i need free thi...

Specializing on a templated type
Hello, Is there any way of specializing a templated function on a type which is itself templated? Here's the simplest example of the problem I can think of. Say I have written an implementation of the identity function as follows, template <class T> T identity(T x) { return x; } and for some reason I want to specialize this function for the following templated type, so that instead of returning the entire structure, the identity function only returns the single value within the structure. template <class T> struct Foo { T foo; }; Is there any way I can do something lik...

type conversion needed
This is a multi-part message in MIME format. ------=_NextPart_000_0001_01C3AE05.7BFCCB20 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit How to convert bytea data type to character varying and vice versa looking forward for ur assistance kishore ------=_NextPart_000_0001_01C3AE05.7BFCCB20 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META HTTP-EQUIV=3D"Content-Typ...

Special DNS needs
Hello! I have the following setup: I've installed a Bind DNS server at home that manages my internal Hostnames(*.lan). I also have a server(IP: 192.168.0.200) that uses dyndns to have a static hostname(*.dyndns.org). Unfortunally I can't connect from my .lan to the outside Ip of the server, so any connection to *.dyndns.org fails. It is possible to configure bind in a way that it resolves *.dyndns.org to 192.168.0.200 instead to the public Ip? If so, can anyone provide an example configuration? Thanks, Henning In article <c900dd$1ash$1@sf1.isc.org>, Henning ...