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

Static and global constructors

Hi,

I'm wondering if there is any order in the calling of global and static
member objects' constructors which occurs before a C++ program can run.
For example, what if I wish to use static member variable objects in a
global variable's constructor? I was thinking that perhaps C++
guarantees to construct all global variable objects first or all static
member variable objects first. Is this at all the case?

Thanks,

Richard Hayden.
Jul 22 '05 #1
5 2664
Richard Hayden <ra***@doc.ic.ac.uk> wrote in news:ci23o8$pvq$1
@sparta.btinternet.com:
Hi,

I'm wondering if there is any order in the calling of global and static
member objects' constructors which occurs before a C++ program can run.
For example, what if I wish to use static member variable objects in a
global variable's constructor? I was thinking that perhaps C++
guarantees to construct all global variable objects first or all static
member variable objects first. Is this at all the case?

Thanks,

Richard Hayden.


Statics are constructed in the order in which they appear in each
translation unit (single compiled file and what it #includes). Order of
initialization between translation units is implementation defined. If
you need to be able to use global objects across translation units, you
either have to use a trick like "nifty counter" (google to find out what
it is) or use a singleton object that creates it on demand.

Gregg
Gregg
Jul 22 '05 #2
Gregg <gr***@invalid.invalid> wrote in
news:Xn*********************************@207.69.15 4.203:
Statics are constructed in the order in which they appear in each


By "order in which they appear", I meant "order in which the objects are
defined". The order they are declared is immaterial to construction order.

Gregg
Jul 22 '05 #3
Gregg wrote:

Order of
initialization between translation units is implementation defined.


It's unspecified, not implementation defined.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #4
Pete Becker <pe********@acm.org> wrote in news:41***************@acm.org:
Gregg wrote:

Order of
initialization between translation units is implementation defined.


It's unspecified, not implementation defined.


What's the difference? Isn't a given implementation deterministic in this
regard?

Gregg
Jul 22 '05 #5
Gregg wrote:

What's the difference? Isn't a given implementation deterministic in this
regard?


The difference is that implementation defined means that a conforming
implementation must document what it does. Yes, it's deterministic, but
typically far too complex for anyone to want to try to figure out.
Objects in libraries are only linked when the linker determines that
they're needed; sometimes that requires multiple passes through the list
of libraries.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #6

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

Similar topics

13
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. ...
2
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
8
by: Markus Henschel | last post by:
Hello, this is a test case of something I just can't explain myself: ...
3
by: noleander | last post by:
Im getting a runtime error because Ive got a static object that is not properly initialized. ---------- header file xxx.hpp --------------- class SomeClass { SomeClass() { .. constructor here...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
2
by: Pegasus | last post by:
Let's suppose that, under a particular platform, the compiler doesn't initialize correctly the global constructors and destructors at startup. The compiler is psp-gcc. I have written two...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.