Daniel Petrie <[EMAIL PROTECTED]> wrote:
> Thank you very much for the pointer [to the module initialization
> helper class technique described in Scott Meyers "Effective C++"
> as "Ensure that global objects are initialized before they're
> used "].   Perhaps I do not fully understand this, but doesn't
> this have the adverse effect of  forcing every class that
> has a static to always be linked into the application?
> That is doesn't this force artifical dependencies
> on any class with a static member which is initialized in the
> module initializer?  We have a number of users who are very
> concerned with code foot print size and I would not want to cause
> such dependencies that make the executables bigger than needed by
> linking in additional classes.  In fact, I would like to put
> some effort into cleaning up some of the dependencies that we
> have so that executables can be smaller then they are today.

You're correct, this technique of using a file static instance
of a module initializer class does mean that any code that uses
the module (i.e. includes the header file for the module) will now
depend on all the static objects that it is responsible for
initializing. If significant use of the module meant depending
on these static objects anyway, then you haven't made things any
worse.

On the other hand, if you apply this technique to a module that
has several independent parts (i.e. it really should be split
into several independent modules), you will increase the coupling
as the initializer class depends on all the parts.

If reducing dependency is your main focus, then another approach
may be better: replacing global static objects with file static
instances and a static member function (or plain non-member function)
that simply returns this instance:

   ClassX& SomeClass::GetTheClassXInstance()
   {
      static ClassX instance_of_x;

      return (instance_of_x);
   }

This technique is good for removing coupling. If you don't invoke
the GetTheClassXInstance() function, you don't depend on the static
object. This doesn't come for free: the compiler has to generate
code to call the constructor the first time the function is called.
It is as though you had written the function like this:

   ClassX& SomeClass::GetTheClassXInstance ()
   {
      static ClassX instance_of_x;

      // this will be initialized to false before any constructors run
      static bool is_x_constructed; 

      if (!is_x_constructed)
      {
         Instance_of_x.ClassX();        // call constructor
         Is_x_constructed = true;
      } 

      return (instance_of_x);
   }

You can minimize the runtime penalty in any class or function that uses
ClassX frequently by storing the reference returned by GetTheXInstance()
in a local or member variable instead of simply calling GetTheXInstance()
every time you need to use the static ClassX object.

   void SomeFunction()
   {
      ClassX& theX = SomeClass::GetTheClassXInstance ();

      // code using theX
   }


_______________________________________________
sipxtapi-dev mailing list
[email protected]
List Archive: http://list.sipfoundry.org/archive/sipxtapi-dev/

Reply via email to