473,414 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

how to replace function-scoped static singleton

Hello,

how to I replace singleton classes using function scope static
variables with one that doesn't use function scope static variables?:

class Foo {
public:
static Foo &instance();
virtual ~Foo();
...
private:
Foo();
Foo(const Foo&);
Foo & operator=(const Foo&);
};

----------------------------------
Foo &Foo::instance()
{
static Foo& theInstance;
...
return theInstance;
}
Nov 19 '08 #1
3 2279
Bob Doe wrote:
how to I replace singleton classes using function scope static
variables with one that doesn't use function scope static variables?:
What is the problem? What are you trying to replace it with?
Anything in particular? "Not using function scope" is not much
of a specification. Have you looked at possible implementations
of the Singleton pattern? Try googling it. Try looking in some
smart books (like the GoF one). Try looking in the archives.

This has been discussed so many times that it just doesn't need
to be repeated, honestly.
>
class Foo {
public:
static Foo &instance();
virtual ~Foo();
...
private:
Foo();
Foo(const Foo&);
Foo & operator=(const Foo&);
};

----------------------------------
Foo &Foo::instance()
{
static Foo& theInstance;
...
return theInstance;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 19 '08 #2
Bob Doe <Du*********@gmail.comkirjutas:
Hello,

how to I replace singleton classes using function scope static
variables with one that doesn't use function scope static variables?:

class Foo {
public:
static Foo &instance();
virtual ~Foo();
Not much point to have public virtual dtor for a singleton object which
is never destroyed ;-) But it does not hurt, of course.
...
private:
Foo();
Foo(const Foo&);
Foo & operator=(const Foo&);
};

----------------------------------
Foo &Foo::instance()
{
static Foo& theInstance;
This won't compile.

Function scope static variables are a proven method for creating
singletons. If this does not work for you, you should provide some
explanation about your worries (e.g. multithreading concerns, memory leak
alarms, etc...)

Paavo

Nov 19 '08 #3
On Nov 19, 10:25 pm, Paavo Helde <pa...@nospam.please.orgwrote:
Bob Doe <DumpForJ...@gmail.comkirjutas:
Hello,
how to I replace singleton classes using function scope static
variables with one that doesn't use function scope static variables?:
class Foo {
public:
static Foo &instance();
virtual ~Foo();

Not much point to have public virtual dtor for a singleton object which
is never destroyed ;-) But it does not hurt, of course.
...
private:
Foo();
Foo(const Foo&);
Foo & operator=(const Foo&);
};
----------------------------------
Foo &Foo::instance()
{
static Foo& theInstance;

This won't compile.

Function scope static variables are a proven method for creating
singletons. If this does not work for you, you should provide some
explanation about your worries (e.g. multithreading concerns, memory leak
alarms, etc...)

Paavo
Nov 20 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Barnes | last post by:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form? Here is the scenario: I have a form that cannot accept apostrophes. I want to use the replace() so...
6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
12
by: Brian | last post by:
I want to use regxp to check that a form input contains at least 1 non-space charcter. I'd like to only run this if the browser supports it. For DOM stuff, I'd use if (documentGetElementById) {}...
3
by: Dave | last post by:
I have an Access 2K application that is distributed to about a dozen users (all with identical NT environments and identical Access versions, object libraries and service packs). I am using the VBA...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
4
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used...
12
by: Michael | last post by:
In PHP there is a function called str_replace (http://php.net/str_replace). Basically you can freed in two strings and a "subject" string. Then it goes through the subject string searching for...
10
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.