473,587 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: Order of construction for objects

Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified. But I am in a situation, where I need to
guarantee, that one object is created before others (not all others,
just all objects of a specified type). I came up with the following
solution, but I am not sure, if it does solve my problem:

class first
{
};

class A
{
static first first_obj;
};

int main ()
{
A mya;
}

class A (further implementation omitted) will rely on some
initialization done by class first. Is it guaranteed that this
initialization will be all done, before I use A?

thanks!
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #1
15 1827
Jakob Bieling wrote:
Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified.
Not entirely. Within one translation unit, it is well-defined.
But I am in a situation, where I need to guarantee, that one object is
created before others (not all others, just all objects of a specified
type).


The typical way to do this is by using the singleton pattern. Something like
this:

class first
{
static first& instance()
{
static first inst;
return inst;
}

private:
first() {}

// prevent copying
first(first&);
void operator=(first &);
};

Then you only use the object through the instance() member function. This
guarantees that the object gets created in the moment it is needed the
first time.

Jul 23 '05 #2
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d7******** *****@news.t-online.com...
Jakob Bieling wrote:
Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified.


Not entirely. Within one translation unit, it is well-defined.
But I am in a situation, where I need to guarantee, that one object
is
created before others (not all others, just all objects of a
specified
type).


The typical way to do this is by using the singleton pattern.
Something like
this:

class first
{
static first& instance()
{
static first inst;
return inst;
}

private:
first() {}

// prevent copying
first(first&);
void operator=(first &);
};

Then you only use the object through the instance() member function.
This
guarantees that the object gets created in the moment it is needed the
first time.

To be more exact, I will not need the object itself .. it only
contains a ctor and a dtor, which take care of initialization and
cleanup. And that initialization is needed for class A.

I could do the initialization at startup before anything else, but I
would like to automate this.

Thanks for the help!
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #3
"Jakob Bieling" <ar************ ****@rot13.com> wrote in message
news:d7******** *****@news.t-online.com
Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified.
It is unspecified if they are in different translation units. If they are in
the same translation unit, then construction is in the same order as
definition. Section 3.6.2/1 of the standard:

"Objects with static storage duration (3.7.1) shall be zero-initialized
(8.5) before any other initialization takes place. Zero-initialization and
initialization with a constant expression are collectively called static
initialization; all other initialization is dynamic initialization. Objects
of POD types (3.9) with static storage duration initialized with constant
expressions (5.19) shall be initialized before any dynamic initialization
takes place. Objects with static storage duration defined in namespace scope
in the same translation unit and dynamically initialized shall be
initialized in the order in which their definition appears in the
translation unit."
But I am in a situation, where I need to
guarantee, that one object is created before others (not all others,
just all objects of a specified type). I came up with the following
solution, but I am not sure, if it does solve my problem:

class first
{
};

class A
{
static first first_obj;
};

This only declares first_obj, where do you define it?
int main ()
{
A mya;
}

class A (further implementation omitted) will rely on some
initialization done by class first. Is it guaranteed that this
initialization will be all done, before I use A?

Section 3.6.2/3 of the standard:

"It is implementation-defined whether or not the dynamic initialization
(8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the
first statement of main. If the initialization is deferred to some point in
time after the first statement of main, it shall occur before the first use
of any function or object defined in the same translation unit as the object
to be initialized.31) [Example:
// – File 1 –
#include "a.h"
#include "b.h"
B b;
A::A(){
b.Use();
}

// – File 2 –
#include "a.h"
A a;

// – File 3 –
#include "a.h"
#include "b.h"
extern A a;
extern B b;
int main() {
a.Use();
b.Use();
}
It is implementation-defined whether either a or b is initialized before
main is entered or whether the initializations are delayed until a is first
used in main. In particular, if a is initialized before main is entered, it
is not guaranteed that b will be initialized before it is used by the
initialization of a, that is, before A::A is called. If, however, a is
initialized at some point after the first statement of main, b will be
initialized prior to its use in A::A. ]"

Section 9.4.2/7:

"Static data members are initialized and destroyed exactly like non-local
objects (3.6.2, 3.6.3)."

From all this, we may conclude: the initialization of

static first first_obj;

"shall occur before the first use [in main()] of any function or object
defined in the same translation unit as the object to be initialized [i.e.,
first_obj]." So if first_obj and A are defined in the same translation unit,
first_obj must be initialised before the first use of A in main(). At least
that is how it appears to me.

--
John Carson

Jul 23 '05 #4
Jakob Bieling wrote:
Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified. But I am in a situation, where I need to
guarantee, that one object is created before others (not all others,
just all objects of a specified type). I came up with the following
solution, but I am not sure, if it does solve my problem:

class first
{
};

class A
{
static first first_obj;
};

int main ()
{
A mya;
}

class A (further implementation omitted) will rely on some
initialization done by class first. Is it guaranteed that this
initialization will be all done, before I use A?

Yes. However all global objects would be initialised before the invocation of main().

In any case, first_obj will be initialised before any use of class A.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5
Ioannis Vranos wrote:
Yes. However all global objects would be initialised before the
invocation of main().

In any case, first_obj will be initialised before any use of class A.

.... even if class A is defined in some local scope (like a function, method or even a
block). That is, in any scope.
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #6
Jakob Bieling wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d7******** *****@news.t-online.com...
Jakob Bieling wrote:
Hi,

I am aware of the fact, that the order of construction of global
objects is unspecified.


Not entirely. Within one translation unit, it is well-defined.
But I am in a situation, where I need to guarantee, that one object
is
created before others (not all others, just all objects of a
specified
type).


The typical way to do this is by using the singleton pattern.
Something like
this:

class first
{
static first& instance()
{
static first inst;
return inst;
}

private:
first() {}

// prevent copying
first(first&);
void operator=(first &);
};

Then you only use the object through the instance() member function.
This
guarantees that the object gets created in the moment it is needed the
first time.

To be more exact, I will not need the object itself .. it only
contains a ctor and a dtor, which take care of initialization and
cleanup. And that initialization is needed for class A.

I could do the initialization at startup before anything else, but I
would like to automate this.


Well, you can also do that with the singleton. You can put your
initialization into the constructor and the cleanup in the destructor.

Jul 23 '05 #7
"John Carson" <jc************ ****@netspace.n et.au> wrote in message
news:d7******** **@otis.netspac e.net.au...
"Jakob Bieling" <ar************ ****@rot13.com> wrote in message
news:d7******** *****@news.t-online.com
--- A.h ---
class first
{
};

class A
{
static first first_obj;
}; This only declares first_obj, where do you define it?
Oops. It should have been in a different translation unit:

--- A.cpp ---

first A::first_obj;

--- main.cpp ---
int main ()
{
A mya;
}

Section 3.6.2/3 of the standard:

"It is implementation-defined whether or not the dynamic
initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace
scope is done before the first statement of main. If the
initialization is deferred to some point in time after the first
statement of main, it shall occur before the first use of any function
or object defined in the same translation unit as the object to be
initialized.31) [Example:
Since first_obj is in its own translation unit, it may possibly not
be initialized at all?
From all this, we may conclude: the initialization of

static first first_obj;

"shall occur before the first use [in main()] of any function or
object defined in the same translation unit as the object to be
initialized [i.e., first_obj]." So if first_obj and A are defined in
the same translation unit, first_obj must be initialised before the
first use of A in main(). At least that is how it appears to me.


It would be a different translation unit. first_obj is in a
translation unit by itself, with no code of A in it. I should add, that
A actually is a template, and thus the definition of its member
functions are inlined. Just let me create a new example, which more
reflects the actual problem:

--- class.h ---

class API_init
{
API_init() { init_some_API (); }
~API_init() { cleanup_some_AP I (); }
};

template <typename T>
class A
{
static API_init init_obj;

A ()
{
API_function_th at_relies_on_in it ();
}

void func ()
{
other_API_funct ion_that_relies _on_init ();
}
};
--- class.cpp ---

#include "class.h"

API_init A::init_obj;
--- main.cpp ---

#include "class.h"

A a1;

int main ()
{
A a2;
a1.func ();
a2.func ();
}
Will the ctor of API_init be called *before* the ctor of A is
called? If not, or if that is not guaranteed, then it may be possible
that the API I will use is not initialized properly, making subsequent
calls to other API functions fail. And that is basically the problem I
am trying to solve.

Thanks
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #8
"Ioannis Vranos" <iv*@remove.thi s.grad.com> wrote in message
news:1117288986 .240165@athnrd0 2...
Ioannis Vranos wrote:
Yes. However all global objects would be initialised before the
invocation of main().

In any case, first_obj will be initialised before any use of class A.

... even if class A is defined in some local scope (like a function,
method or even a block). That is, in any scope.

Even if class A does not call any member function of first_obj? I
posted a more exact example in reply to John's answer .. so if your 'in
any case' applies to that case as well, my problem would be solved :)

Thanks!
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #9
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d7******** *****@news.t-online.com...
Jakob Bieling wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d7******** *****@news.t-online.com...
Jakob Bieling wrote:

Hi,

I am aware of the fact, that the order of construction of
global
objects is unspecified.

Not entirely. Within one translation unit, it is well-defined.

But I am in a situation, where I need to guarantee, that one object
is
created before others (not all others, just all objects of a
specified
type).

The typical way to do this is by using the singleton pattern.
Something like
this:

class first
{
static first& instance()
{
static first inst;
return inst;
}

private:
first() {}

// prevent copying
first(first&);
void operator=(first &);
};

Then you only use the object through the instance() member function.
This
guarantees that the object gets created in the moment it is needed
the
first time.

To be more exact, I will not need the object itself .. it only
contains a ctor and a dtor, which take care of initialization and
cleanup. And that initialization is needed for class A.

I could do the initialization at startup before anything else,
but I
would like to automate this.


Well, you can also do that with the singleton. You can put your
initialization into the constructor and the cleanup in the destructor.

Uh yes, but 'instance' will never be called, because an instance is
not needed. The class 'first' does not contain any functionality, other
than initializing an API (and cleaning up). I wanted to avoid having a
dummy call in all class A ctors (either to a global function containing
the static object, or the member function as in your example), so I came
up with the static-member-of-class idea.
Since John's post I realized that the cut-down example I gave you,
did not really reflect the problem, so I wrote a new one in reply to
John's answer.
Basically, I want the initialization code to be automatically
executed (ie. no explicit function calls) before I use the API. Same for
the cleanup code: cleanup after I am done with everything. But I assumed
once the first problem is solved, the second will automatically be
solved, because destructions takes place in the reverse order of
construction. Or am I assuming too much?

Thanks!
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #10

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

Similar topics

2
1643
by: John Carson | last post by:
One routinely hears that the order of initialisation of objects with static storage duration in different translation units cannot in general be guaranteed. I have a question about one way to influence the order of initialisation. Suppose that class A contains both a static object and a static member function that refers to that static...
4
1670
by: Jerivix Entadi | last post by:
I'm attempting to create an application to work with a fluid database of people. I'm doing this in a command line, text-based manner. I need to know two things: 1) How do I save data, perhaps a group of objects and their memebers or a few lines of text? and 2) Is there any way to automate object construction? I'm thinking that I can create a...
0
1228
by: Stephan Keil | last post by:
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
3
1655
by: ashaniray | last post by:
Hi, The ISO-C++ spec says that the order of construction for C++ objects is: **************************************************************** .... Initialization shall proceed in the following order: * First, and only for the constructor of the most derived class as
7
2400
by: Raider | last post by:
Are global objects always constructed in order of they declaration? I use one global in the constructor of another one and I want to be sure it works the same with any compiler. The code looks like this: #include <iostream> class Class1 { public:
3
1560
by: Zongjun Qi | last post by:
Hey, In the book <Effective C++>, the author provides an example to prove why we need "pass by reference". I redoed the example, and found something interesting. The codes are: ############################## #include <iostream> class Student{ public:
6
2638
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;
7
3399
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...
7
2488
by: Peter | last post by:
I know the order of construction of member and base class objects. My question is the following: Is the order of evaluation of argument lists for these constructors also defined? E.g. can I assume that the following code is exceptions safe? Assuming that the constructor of A, B or C may throw? Can I assume that B is created after the...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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...
1
7973
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...
0
6626
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...
0
3844
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.