473,853 Members | 1,839 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 1547
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
2171
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
1425
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
2540
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
2444
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
5056
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
4886
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
3855
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
4288
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
5508
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
9898
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
11020
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...
1
10735
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,...
1
7907
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
5736
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
5934
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4549
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
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3180
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.