Where Global Object's constructor can be useful.

R

Rajesh

I read Global Object's constructor will be called before main()
function;

In which situation it can be really helpful? Is it good practice use
Global object and its constructor ?

Thanks,
Rajesh.S
 
P

peter koch

I read Global Object's constructor will be called before main()
function;

In which situation it can be really helpful? Is it good practice use
Global object and its constructor ?

Thanks,
Rajesh.S

Whenever you need something to be done automatically by your object.
Some kind of registration is one application. Another is if you need
to initialise something but don't want the users to do so themselves:
when programming in windows, initialising of COM is a nice example.

/Peter
 
S

Saeed Amrollahi

I read Global Object's constructor will be called before main()
function;

In which situation it can be really helpful? Is it good practice use
Global object and its constructor ?

Thanks,
Rajesh.S

Hi
As you mentioned, global objects have been constructed before calling
main() and they will be destructed after main().
of course it it true for all non-local objects included global,
namespace and class static members.
Sometimes it is necessary to have global objects. For example when an
object should be shared among several translation units
like an object to manage database connection. I mean sometimes it is
more than helpful, it is necessary.
Of course in most cases you can use reduce the number of global
objects by using static data members and it is better practice.

Regards,
Saeed Amrollahi
 
P

Puppet_Sock

I read Global Object's constructor will be called before main()
function;

In which situation it can be really helpful? Is it good practice use
Global object and its constructor ?

For a discussion of when to use globals, and how to reduce
them, and why to reduce them, see _Code Complete (2nd Edition)_
by Steve McConnell. I have found this to be an excellent book.
(Few typos, but eh? What are you going to do?)
Socks
 
J

Juha Nieminen

Puppet_Sock said:
For a discussion of when to use globals, and how to reduce
them, and why to reduce them, see _Code Complete (2nd Edition)_
by Steve McConnell. I have found this to be an excellent book.
(Few typos, but eh? What are you going to do?)

It's not necessary for the object to be global. It's enough for it to
be inside a namespace (eg. a nameless one).

For example std::cout is constructed before main() is called.
 
J

James Kanze

It's not necessary for the object to be global. It's enough
for it to be inside a namespace (eg. a nameless one).

The question is what he means by "global". The only use of
"global" in the standard (I think) is in the "global namespace";
the "outermost declarative region of a translation unit". It's
a namespace scope. Which means in fact that it is a scope, and
not an object lifetime (although object lifetime is in some
cases dependent upon the scope in which the object is defined).
And of course, if he's using global in this sense, then there
are a lot more objects which will be constructed before main.
(Except that the word he wants is probably initialized, rather
than constructed.)

The rule (the actual rule, not the formal one spelled out in the
standard) is that objects with static lifetime and non-local
scope are constructed either before main, or in the case of
dynamically loaded objects, before returning from dlopen or its
Windows equivalent. An object has static lifetime if:

-- it is defined at namespace scope, or
-- it is declared static (regardless of scope).

(Again, this is not the exact wording of the standard, but is, I
think, a fairly accurate summary.)
For example std::cout is constructed before main() is called.

std::cout and company are very special cases, in that the
standard places some additional constraints on them. (Among
other things, they are never destructed.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top