473,239 Members | 1,832 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,239 software developers and data experts.

Alternative was to implement a Singleton Design Pattern

Hello,

currently I am reading the book "Effective C++ Third Edition" from Scott
Meyers. While Reading Item 4 (Make sure that objects are initialized
before they're used) I got an idea how to improve the way to implement
the Singlton Design Pattern.

This is the way I used to do it:

Singleton.h
----------

class Singleton {
public:
static Singleton* getInstance() {
if (!m_instance) {
m_instance = new Singleton();
}

return m_instance;
}

private:
Singleton() { }
~Singleton() { }
static Singleton* m_instance;
}

Singleton.cpp
-------------

Singleton* Singleton::m_instance = 0;


This is the way I am planning to do it in future:

Singleton.h
-----------

class Singleton {
public:
static Singleton* getInstance() {
static Singleton instance;
return &instance;
}

private:
Singleton() { }
~Singleton() { }
};

(Source is only showing the concept. I didn't compile it.)
I see the following pros:
- No potentional memory leak when process terminates.
- Less source code required.
- Not necessarely a .cpp file required for the static member.

Are there any cons I am missing?

Thanks for your thoughts,
Daniel Kay
Sep 3 '06 #1
2 2606
Daniel Kay wrote:
This is the way I am planning to do it in future:

Singleton.h
-----------

class Singleton {
public:
static Singleton* getInstance() {
static Singleton instance;
return &instance;
}

private:
Singleton() { }
~Singleton() { }
};

(Source is only showing the concept. I didn't compile it.)
I see the following pros:
- No potentional memory leak when process terminates.
- Less source code required.
- Not necessarely a .cpp file required for the static member.

Are there any cons I am missing?
See Alexandrescu's "Modern C++ Design". It discusses singletons in more
detail than you'd ever want to know, and in particular, he discusses
this idiom (he calls it a "Meyers Singleton", since Meyers is the
earliest cite he found it in, I guess).
Sep 3 '06 #2
Daniel Kay wrote:
I see the following pros:
- No potentional memory leak when process terminates.
- Less source code required.
- Not necessarely a .cpp file required for the static member.
singleton would prefferably be implemented as a template
so this is not realy a benefit
>
Are there any cons I am missing?
I don't think there is a single implementation that satisfies
all possible requirements.

few cons might arrise

- No control over singleton's liffetime, can't delete it
see http://www.research.ibm.com/designpa...s/ph-jun96.txt
<no link intentional>
- Singleton must have default constructor
- if Singleton's constructor throws you might not be able to recover
- I can do
delete Singleton::getInstance()
invoking undefined behaviour, bether return reference
instead of a pointer

otherwise it's fine :)

Sep 6 '06 #3

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

Similar topics

14
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
4
by: Neil Zanella | last post by:
Hello, I would be very interested in knowing how the following C++ multi-instance singleton (AKA Borg) design pattern based code snippet can be neatly coded in Python. While there may be...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
14
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
9
by: FluffyCat | last post by:
To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern. http://www.fluffycat.com/PHP-Design-Patterns/Singleton/ In the...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
5
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.