473,748 Members | 4,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Destruction order (e.g. singletons)

Hi all,

consider a class

class X {
static X& OneX() { static X sgl1; return sgl1; }
static X& TheX() { static X sgl2(OneX()); return sgl2; }
};

and two source files with global variables

// a.cpp
X a1;
X a2(X::TheX());

// b.cpp
X b1;
X b2(X::TheX());

As I understand it, two valid construction orders are
"a1 sgl2 sgl1 a2 b1 b2" and "b1 sgl2 sgl1 b2 a1 a2".

Now, two questions:
1. Is "a1 b1 sgl2 sgl1 b2 a2" also a valid construction order
(i.e. is it guaranteed that the global objects of a
translation unit are all constructed before any global object
of another translation unit)?
2. I would find it natural if the destruction order follows
always the reverse construction order.
But what does the standard say about the destruction order
(esp. for sgl1 and sgl2)? (And do current compiler follow
the standard in that point;-?)

Thanks, Stephan
Jul 22 '05 #1
0 1233

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

Similar topics

5
3162
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
4
575
by: Dave | last post by:
Hello all, I have made a class into a singleton by having a static get() method which returns a pointer to the one-and-only instance. This method creates the instance on the heap if it hasn't already done so in a prior call. Constructors are private so that the class may not be instaniated from outside. As I understand it, this is the standard approach and is nothing new. I have no need to ever destroy this singleton and re-create a...
11
1685
by: Tito | last post by:
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...
3
3157
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;
0
242
by: Yevgeny Menaker | last post by:
I am struggling with the following: We have legacy classes written in C++ that are compiled to static libraries. Some of these classes are singletons. In order to make these classes available to C#, we wrapped them with Managed C++. The wrapper classes works fine in most of the cases. However, if they are used in ASP.NET Web Applications a problem may arise in the following scenario: Step 1: The first Web Application starts in its own...
11
2358
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
2643
by: subramanian | last post by:
Suppose I have the following class:(shown only partially. Assume proper ctors and dtors): class Date { ... static Date temp_date; static Date another_date;
2
4315
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
7
3411
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case class is derived as virtual? How does the order of construction/destruction is impacted if the base class is derived as virtual or non virtual? just see the example below.
0
8826
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
9534
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
9366
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...
0
9241
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...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
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
2211
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.