473,763 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alexei's singletons

I have two questions about the singletons' chapter of Alexei Alexandrescu's
"C++ Modern Design".

1. In the beginning of the chapter Alexei states that a "singleton" class
implementation made of static member functions has the problem that the
functions are not virtual, so that you have to touch the class' code in
order to change the behaviour. But, how is a singleton meant to be inherited
from? Is not the concrete class of the unique instance already hardcoded in
the singleton's implementation?

2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other
singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?

Thanks,
Tito
Jul 22 '05 #1
11 1688
* "Tito" <ti************ *************** @inicia.es> schriebt:

I have two questions about the singletons' chapter of Alexei Alexandrescu's
"C++ Modern Design".
That must be Andrei's little known brother.

1. In the beginning of the chapter Alexei states that a "singleton" class
implementation made of static member functions has the problem that the
functions are not virtual, so that you have to touch the class' code in
order to change the behaviour. But, how is a singleton meant to be inherited
from?
As with any other class.

Is not the concrete class of the unique instance already hardcoded in
the singleton's implementation?
It would be if you used the static member function pattern, and that's
the point.
2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other
singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?


I'm not sure what you're referring to here.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
"Tito" <ti************ *************** @inicia.es> wrote in message news:<2h******* *****@uni-berlin.de>...
[snip]
2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other
singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?


The objects contained in the pTrackerArray are deleted in AtExitFn(),
but the array itself is not. Since we do not know how many objects are
contained in pTrackerArray when AtExitFn() is called, we don't know
when to delete the array. But realloc(), as explained in the footnote
on page 143, can behave as both malloc and free. If you call it with
size>0 it behaves as malloc, and if you call it with size=0 it behaves
as free. This means that we can just call realloc() with the number of
objects in the array, and when the number of objects in the array
reaches zero the array is automagically deleted.

T.
Jul 22 '05 #3
> 2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other
singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?


Quick correction: Andrei, not Alexei.

Read carefully paragraph starting with "There is only one instance of
the Tracker type..."

It says, as I understand it, that TrackerArray is itself a singleton.
Yet it must be used by a function that manages singletons. That means
that you cannot apply singleton mechanism we're devising to the
TrackerArray.
The way Andrei went about solving this problem was to
use a contiguous c-style array, not a container, and manage the
allocation/deallocation of the whole array with low-level routines
that are guaranteed to work at any time.
AtExitFn does use delete() on LifetimeTracker object, as SetLongevity
creates them with new. But we're talking about an array of pointers to
LifetimeTracker objects whose size is controlled by reallocate.

HTH,
Andy.
Jul 22 '05 #4
"Tito" <ti************ *************** @inicia.es> wrote in message
news:2hhumuFdhf 3oU1@uni-
1. In the beginning of the chapter Alexei states that a "singleton" class
implementation made of static member functions has the problem that the
functions are not virtual, so that you have to touch the class' code in
order to change the behaviour. But, how is a singleton meant to be inherited from? Is not the concrete class of the unique instance already hardcoded in the singleton's implementation?
If you inherit from a singleton, it means that you're creating many
instances of the singleton, each with a different behavior, and it's no
longer a singleton. Just my char(2) :).

2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other singletons. I do not understand that, because they are cleanly deleted on
atexit(). Can someone explain it to me?


Don't know what you're talking about. My guess is that new T calls
::operator new to get memory (which may call std::malloc) and then a
constructor, so it has something to do with the constructor call. Similar
delete t calls the destructor the ::operator delete.
Jul 22 '05 #5
> > 1. In the beginning of the chapter Alexei states that a "singleton"
class
implementation made of static member functions has the problem that the
functions are not virtual, so that you have to touch the class' code in
order to change the behaviour. But, how is a singleton meant to be inherited from?


As with any other class.


Yes, but then you are having a subclass of your singleton without any
protection
against multiple instantiation:

Singleton* s1 = new DerivedSingleto n;
Singleton* s2 = new DerivedSingleto n; // !!!
Is not the concrete class of the unique instance already hardcoded in
the singleton's implementation?


It would be if you used the static member function pattern, and that's
the point.


No, it's already hardcoded in the usual way:

static Singleton& getInstance() {
if (!pInstance)
pInstance = new Singleton; // class hardcoded!!
return *pInstance;
}

Tito
Jul 22 '05 #6
> > 1. In the beginning of the chapter Alexei states that a "singleton"
class
implementation made of static member functions has the problem that the
functions are not virtual, so that you have to touch the class' code in
order to change the behaviour. But, how is a singleton meant to be

inherited
from? Is not the concrete class of the unique instance already hardcoded

in
the singleton's implementation?


If you inherit from a singleton, it means that you're creating many
instances of the singleton, each with a different behavior, and it's no
longer a singleton. Just my char(2) :).


Exactly, that is one of the things, but, in addition,
- the class that is instantiated is hardcoded into the static Instance()
member function, so that you will get always the same behaviour by calling
Singleton::Inst ance(), no matter how you derive the class.
- Andrei makes the constructors of the Singleton private, so you *cannot*
derive from Singleton.

Tito
Jul 22 '05 #7
> > 2. We he presents his SetLongevity utility, he uses realloc instead of
new/delete, arguing that if not doing so, the data structure that
SetLongevity maintains would suffer of the same longevity problems as other singletons. I do not understand that, because they are cleanly deleted on atexit(). Can someone explain it to me?
Quick correction: Andrei, not Alexei.

Read carefully paragraph starting with "There is only one instance of
the Tracker type..."


Yes, now I have read it too many times ;)
It says, as I understand it, that TrackerArray is itself a singleton.
Yet it must be used by a function that manages singletons. That means
that you cannot apply singleton mechanism we're devising to the
TrackerArray.


Yes, that is what I understand when reading the paragraph. But that is my
point: the TrackerArray is not a singleton, so I do not think that it is
"exposed to all the Singleton problems". It is a global variable in the
unnamed space, so it should no be accessed by any other code but the one
contained inside of SetLongevity, which is the only responsible of
maintaining the data structure.

More precisely, I do not understand:
- why is pTrackerArray "exposed to all the Singleton problems just
discussed",
- what is the problem in: "SetLongevi ty must be available at any time, yet
it has to manage private storage.",
- how malloc & co. come to help.

Tito
Jul 22 '05 #8
See my answer to Andy Venikov.

Tito
Jul 22 '05 #9
* "Tito" <ti************ *************** @inicia.es> schriebt:
1. In the beginning of the chapter Alexei states that a "singleton" class implementation made of static member functions has the problem that the
functions are not virtual, so that you have to touch the class' code in
order to change the behaviour. But, how is a singleton meant to be inherited from?


As with any other class.


Yes, but then you are having a subclass of your singleton without any
protection
against multiple instantiation:

Singleton* s1 = new DerivedSingleto n;
Singleton* s2 = new DerivedSingleto n; // !!!


Whether you have protection against that depends on the class.

Is not the concrete class of the unique instance already hardcoded in
the singleton's implementation?


It would be if you used the static member function pattern, and that's
the point.


No, it's already hardcoded in the usual way:

static Singleton& getInstance() {
if (!pInstance)
pInstance = new Singleton; // class hardcoded!!
return *pInstance;
}


I read that as "yes, it's hardcoded that way"?

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #10

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

Similar topics

5
3164
by: stephan beal | last post by:
Good morning, C++ users, i've been hesitating to post this, primarily because i know that most of you here are *way* ahead of me in C++ and i'm a little embarassed about the possibility of some gross errors in what i'll be posting... That said, please go easy on me. :) About 2 weeks ago i wrote a paper, called Context Singletons, where i discuss some of the uses of context-specific Singleton-like objects. During
3
3160
by: Dominik Rau | last post by:
Hi. I've got the following problem here: In my application, I use a lot of Singletons, that are implemented as described in Gamma et al. (shortened): //.h class Singleton{ public: static Singleton* the(); private: static Singleton* _instance;
8
4450
by: 6tc1 | last post by:
Hi all, I'm having a problem where in my solution that contains multiple projects - I instantiate a singleton class in one assembly and then if another assembly tries to use that singleton class another instance of it is created. Basically: --Assembly 1 (the executable)-- -Main.cs-
11
2362
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure similar to a database or XML representation. Rules to modify the stored data will be executed, then the data will be transformed into an output format. Think something similar to FormatA -> XML -> Manipulate XML -> FormatB
6
1387
by: Steven Watanabe | last post by:
PEP 8 says, "Comparisons to singletons like None should always be done with 'is' or 'is not', never the equality operators." I know that "is" is an identity operator, "==" and "!=" are the equality operators, but I'm not sure what other singletons are being referred to here. Also, I've seen code that does things like: if foo is 3: if foo is not '':
5
1495
by: Omega | last post by:
I'm interested in seeing a bit of discussion about using singletons in ASP.NET 2.0. Currently I've designed a singleton that gets a reference to it's single instance stored inside the ASP.NET application object. This is done to persist and make available live information across multiple sessions. I've read a little bit (almost nothing) about how singletons don't play nicely in clustered scenarios and would like to hear more on the
6
10383
by: =?Utf-8?B?R29yZG8=?= | last post by:
Hello everyone, I've been trying for some time now to move to C++/CLI, but I have several large legacy C++ static libraries I need to use. When I set up a simple solution with a C++/CLI Winforms app and a C++ native static library, they work well together _unless_ I have, it seems, any static variables defined in any function in the native library. The libraries I'm trying to use all have Meyers Singletons in them, so they need to have...
7
1809
by: adam.timberlake | last post by:
I was reading an article on TalkPHP (http://www.talkphp.com/ showthread.php?t=1304) about singletons but I'm afraid I don't understand why I need to use them. I understand how to code them perfectly, it's just the theory I'm having some problems with. I did try searching on Wikipedia but it didn't yield any satisfactory reasoning, for me. Could someone explain it to me in basic terms, please. I initially came across singletons when I...
12
1429
by: Craig Allen | last post by:
Hey, forgive me for just diving in, but I have a question I was thinking of asking on another list but it really is a general question so let me ask it here. It's about how to approach making singletons. Background: I've been programming in python seriously for about a year now, maybe a little longer depending how you count, and the system I am making is sophisticated enough that I've had to enter into a few idioms which were beyond my...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.