473,598 Members | 3,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where Global Object's constructor can be useful.

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
Jul 22 '08 #1
6 1533
On 22 Jul., 13:35, Rajesh <mailtosraj...@ gmail.comwrote:
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
Jul 22 '08 #2
On Jul 22, 2:35*pm, Rajesh <mailtosraj...@ gmail.comwrote:
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
Jul 22 '08 #3
On Jul 22, 7:35*am, Rajesh <mailtosraj...@ gmail.comwrote:
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
Jul 22 '08 #4
Thank you very much for your replies.

Jul 23 '08 #5
Puppet_Sock wrote:
On Jul 22, 7:35 am, Rajesh <mailtosraj...@ gmail.comwrote:
>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?)
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.
Jul 23 '08 #6
On Jul 24, 1:11 am, Juha Nieminen <nos...@thanks. invalidwrote:
Puppet_Sock wrote:
On Jul 22, 7:35 am, Rajesh <mailtosraj...@ gmail.comwrote:
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?)
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.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 24 '08 #7

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

Similar topics

2
2159
by: Martin Lucas-Smith | last post by:
I have a class from within which other classes are called. In the constructor, I want to create an instance of a database connection, so that this database can be called elsewhere. <?php # Declare a class foo class foo { # Constructor
0
1414
by: pavana | last post by:
Hi, I have a class with a constructor which does some socket initialisation. I need to call the constructor before the dll is loaded. For this, I do something like this in the code (written in C++): wsa_startup SWS; //wsa_startup is the class. This should ideally call the constructor of wsa_startup class. When I
21
2502
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default constructors class MyClass { Foo foo; // foo and bar require default constructors Bar bar; public:
7
2432
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a double, I have to define a global operator+ (or -*/) and special function classes. --- source begins here ---
17
5023
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
8
4857
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical differences in the way both the objects are handled by IIS. Are both objects stored in different memory spaces? I can access both the objects in my web page. I will be grateful if some one can help me understand the difference.
11
3814
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
10
4252
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version. Because of this, I am unable to store these settings in the App.Config file, as this gets updated every time the application does, and there doesn't appear to be a way of preventing this. Most of my application settings are kept in the...
112
5395
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
7991
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8395
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
8398
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...
1
8050
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8265
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...
0
6719
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5850
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
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2412
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

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.