473,396 Members | 2,109 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,396 software developers and data experts.

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 1654
* "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:2hhumuFdhf3oU1@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 DerivedSingleton;
Singleton* s2 = new DerivedSingleton; // !!!
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::Instance(), 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: "SetLongevity 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 DerivedSingleton;
Singleton* s2 = new DerivedSingleton; // !!!


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
> > > > 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 DerivedSingleton;
Singleton* s2 = new DerivedSingleton; // !!!


Whether you have protection against that depends on the class.


No, if you let your singleton be inherited from, then anybody can create
multiple instances of it.
> 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"?


How do you build a singleton that instantiates a class that is not hardcoded
in its implementation?

Tito
Jul 22 '05 #11
* "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 DerivedSingleton;
Singleton* s2 = new DerivedSingleton; // !!!
Whether you have protection against that depends on the class.


No, if you let your singleton be inherited from, then anybody can create
multiple instances of it.


That turns out not to be correct.

A simple exercise for you: disprove your own statement.
How do you build a singleton that instantiates a class that is not hardcoded
in its implementation?


Read up on and (not the least) try out templates in C++.

Essentially that's what Andrei's SingletonHolder (if I remember the name
correctly) is all about.

--
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 #12

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

Similar topics

5
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...
3
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...
8
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...
11
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...
6
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...
5
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...
6
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...
7
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...
12
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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
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...
0
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...
0
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,...

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.