By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

Singleton desin pattern in C++

P: n/a
Hi,

Anyone knows of a link to a singleton design pattern example in C++
(preferably not using a global variable?).

Jul 23 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
About the Singleton, where can I find a good example in a
multithreading environment without the Double Checked Locking Pattern ?

Is there a solution to this problem ?

Jul 23 '05 #2

P: n/a
Alfonso Morra wrote:
Anyone knows of a link to a singleton design pattern example in C++
www.google.com
(preferably not using a global variable?).


I am sure you can find plenty of different examples there.

V
Jul 23 '05 #3

P: n/a


Victor Bazarov wrote:
Alfonso Morra wrote:
Anyone knows of a link to a singleton design pattern example in C++

www.google.com


Really?, I never thought of that. Seriously though, I have already
searched google on several occasions in the past (including this
morning), there are lots of examples available in Java - but I cannot
seem to locate anything on C++ (-that is, anything that goes beyond the
definition of what a design pattern is etc..).

A singleton is a particularly simple design (if not the simplest), but
rather than rush ahead and "roll my own", I thought I'd find out if an
implementation is already available, from someone who (hopefully) has a
lot more experience in C++ than I do.
(preferably not using a global variable?).

I am sure you can find plenty of different examples there.

V


Jul 23 '05 #4

P: n/a
Alfonso Morra wrote:
I thought I'd find out if an
implementation is already available, from someone who (hopefully) has a
lot more experience in C++ than I do.


If you're interested in a real-world implementation, you may want to take a
look at ACE's source code: http://www.cs.wustl.edu/~schmidt/ACE.html.

Max
Jul 23 '05 #5

P: n/a


Max M. wrote:
Alfonso Morra wrote:
I thought I'd find out if an
implementation is already available, from someone who (hopefully) has a
lot more experience in C++ than I do.

If you're interested in a real-world implementation, you may want to take a
look at ACE's source code: http://www.cs.wustl.edu/~schmidt/ACE.html.

Max


I did look at this several moons ago (2 years to be exact), and I got
slightly initimidated by all the CORBA specific (as well as advanced C++
stuff), I think I'll stay away from it for a while - I want to keep
things relatively simple for now ...

but thanks for your time anyhows ...

Jul 23 '05 #6

P: n/a
Alfonso Morra wrote:
I did look at this several moons ago (2 years to be exact), and I got
slightly initimidated by all the CORBA specific (as well as advanced C++
stuff), I think I'll stay away from it for a while - I want to keep
things relatively simple for now ...


Singletons are merely global variables. And global variables are evil.
It doesn't make them any better to call them 'Singleton design pattern'.

Jul 23 '05 #7

P: n/a
"Alfonso Morra" writes:
Anyone knows of a link to a singleton design pattern example in C++
(preferably not using a global variable?).


Here's a couple links I saved some time back, I don't certify them but they
passed my "I don't like this link" test.

http://www.codeguru.com/Cpp/Cpp/cpp_...ticle.php/c823
http://www.csc.calpoly.edu/~dbutler/.../tutorial.html

I had a third one but it is no longer on the Web.

Depending on how fussily you define global variable, you might be trying to
do the impossible. But I note you don't insist on that aspect.
Jul 23 '05 #8

P: n/a
Alfonso Morra wrote:
I did look at this several moons ago (2 years to be exact), and I got
slightly initimidated by all the CORBA specific (as well as advanced C++
stuff), I think I'll stay away from it for a while


There's definitely no CORBA-specific code in the ACE core library and the
singleton implementation is spread across a few files, but I concede
there's probably little point in studying all the dependencies if you don't
plan to use the library.

Max
Jul 23 '05 #9

P: n/a
> Singletons are merely global variables. And global variables are evil.
It doesn't make them any better to call them 'Singleton design pattern'.


Are you saying this to be inflammatory or just to show your lack of
understanding? To me this sounds like a flippant silly comment. Why
did you say it?

Stuart

Jul 23 '05 #10

P: n/a
You might not want to use ACE at all, but it may still be worthwhile to
look through it. There are a few (couple of hundred slides :)
presentations on ACE and patterns etc from one of the courses that Dr.
Schmidt teaches at Vanderbilt (look up Component Middleware or
Operating Systems - II). If you have questions (not trivial) you might
actually want to contact Dr. Doug Schmidt. I am sure he will be able to
answer a couple of important questions.

-vijai.

Jul 23 '05 #11

P: n/a
Stuart MacMartin wrote:
Singletons are merely global variables. And global variables are evil.
It doesn't make them any better to call them 'Singleton design pattern'.


Are you saying this to be inflammatory or just to show your lack of
understanding? To me this sounds like a flippant silly comment. Why
did you say it?


And the difference is??? Don't blather, try it in one sentence.

Jul 23 '05 #12

P: n/a
Alfonso Morra wrote:

A singleton is a particularly simple design (if not the simplest), but
rather than rush ahead and "roll my own", I thought I'd find out if an
implementation is already available, from someone who (hopefully) has a
lot more experience in C++ than I do.


After reading some of Alexandrescus' Modern C++ Design, I can assure you that it's not the simplest design by far!

Check out
http://safari.informit.com/?x=1&mode...1-70431-5/ch06

And Loki.

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #13

P: n/a
>> there are lots of examples available in Java - but I cannot
seem to locate anything on C++ (-that is, anything that goes beyond the
definition of what a design pattern is etc..).

This C++ link I have found through google,
http://gethelp.devx.com/techtips/cpp.../10min0200.asp
http://www.codeproject.com/cpp/singletonrvs.asp ---This link also

Jul 23 '05 #14

P: n/a
Mercator schreef:
Singletons are merely global variables. And global variables are evil.
It doesn't make them any better to call them 'Singleton design pattern'.


Wrong. Or do you really think that the single global heap used by new
is evil? Should each use of new/delete require a (matching) heapID?
Wait a minute, that would be one global array of heaps - again a
singleton. See, there is really no escape.

Basically, if my design tells me that a certain entity occurs only
once, then that is a Singleton. You can't fool reality.

HTH,
Michiel Salters

Jul 23 '05 #15

P: n/a
msalters wrote:
You can't fool reality.


At least you are right here. A one-instance Singleton global variable
produces the same coupling as ordinary multi-instance global variables.

Jul 23 '05 #16

P: n/a
Mercator wrote:
msalters wrote:
You can't fool reality.


At least you are right here. A one-instance Singleton global variable
produces the same coupling as ordinary multi-instance global variables.


And why do you think such coupling is necessarily evil? Convoluted
impractically "flexible" designs are also evil.

Jul 23 '05 #17

P: n/a
Bart wrote:
And why do you think such coupling is necessarily evil?
Yes!
Convoluted impractically "flexible" designs are also evil.


Avoid flexibility by using global Singletons?? I beg to differ!

Jul 23 '05 #18

P: n/a
A few additional links:

Design Patterns by
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
This is popularly known as the Gang of 4 book (GoF).
http://www.awprofessional.com/title/0201633612

Interview with Eric Gamma
http://www.artima.com/lejava/article...rinciples.html

Good intro tutorial:
http://www.stardeveloper.com/article...4022804&page=1

Jul 23 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.