Article View: gnu.g++.help
Article #325Re: initialization of objects within a library
From: Bob Martin
Date: Wed, 28 Nov 1990 05:21
Date: Wed, 28 Nov 1990 05:21
82 lines
3182 bytes
3182 bytes
I am posting this because your mail bounced...
In article <3306@danetis.UUCP> you write:
>To ensure that global objects of a library is initialized befor its first
>use, I implemented a counter to maintain the number of translation units
>using it (see "the annotated C++ reference manual section 3.4, Ellis &
>Stroustrup).[example below]
>To my confusion, my library objects are constructed (and destructed) twice !
>How can I reduce constructor calls to only ONE for each global object ??
>
>mail, and I will post a summary
>
>engeln@danetis.uucp
>------------------------------- cut here -- nifty_library.h -----------
>..... junk taken out .....
>
>// counter of translation units
>static nifty_counter nifty;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This constructs nifty. The constructor
for nifty explicity calls the constructor
for foo. That is the cause of the first
aFoo construction by nifty.
>
>#endif nifty_cc
>#endif nifty_h
>------------------------------- cut here -- nifty_library.cc ----------
..... Junk taken out.
// declare global objects to resolve external references
foo aFoo ("aFoo by library startup");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This causes aFoo to be constructed and so the foo::foo constructor
is called. This is the cause of the second construction by
library startup.
..... Junk taken out.
>
>// counter implementation
>nifty_counter::nifty_counter ()
>{
> if (count++ == 0) {
> // initialize global objects
> aFoo.foo ("aFoo by nifty_counter");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Ouch! This makes an explicit call to the contstructor for
foo. You should never explicity call a constructor. It is
the compiler's job to call constructors. This will certainly
cause trouble for you.
> }
>}
>
You should never ever call a constructor or destructor yourself. The
compiler will do these things for you...
Let me recommend that you declare the variable "aFoo" as follows:
foo *aFoo; <--- as a pointer to a foo.
Now in nifty_counter::nifty_counter() you can create a foo as follows:
if (count++ == 0)
{
aFoo = new foo("aFoo by nifty_counter");
// will call foo::foo for you.
}
Then in nifty_counter::~nifty_counter() you can destroy the foo
as follows:
if (--count == 0)
{
delete aFoo; // will call foo:~foo for you.
}
Hope this helps.
--
+-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for |
| rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want |
| uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all |
+----------------------+:R::R::CCC:M:::::M:| the blame. So there. |
Technical Details
Message-ID:
<1990Nov28.052118.11028@clear.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!emory!att!linac!tellab5!balr!clrcom!rmartinReferences:
<3306@danetis.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!emory!att!linac!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: Re: initialization of objects within a library Keywords: c++,library,initialization Message-ID: <1990Nov28.052118.11028@clear.com> Date: 28 Nov 90 05:21:18 GMT References: <3306@danetis.UUCP> Organization: Clear Communications, Inc. Lines: 82