    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Static vs Member Functions</title>
        <link>https://members.accu.org/index.php/articles/574</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #29 - Dec 1998</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c199/">29</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-199/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+199/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Static vs Member Functions</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 27 December 1999 17:23:23 +00:00 or Mon, 27 December 1999 17:23:23 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="section" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>I had the following correspondence with a customer, and, having
put some effort into the explanation, it occurred to me to follow
your example and recycle it into an article for Overload.</p>
<p><span class="emphasis"><em>There is a static member function in
the example you gave me. I don't really understand the details
about how a static function runs under multithreading environments.
Would you please tell me the details?</em></span></p>
<p>Using static data can be a problem in a multithreaded program,
but static functions aren't. Static functions are just a scoping
mechanism to prevent polluting the global namespace.</p>
<p>Here's some example code for discussion:</p>
<pre class="programlisting">
class foo
{
private:
  int my_value;
  // every foo object has one of these
  static int default_value;
  // only one of these in the program
public:
  foo( int new_value )
  { my_value = new_value; }
  foo()
  { my_value = foo::default_value; }
  void change_value( int new_value )
  {my_value = new_value; }

  static void
  set_default_value( int new_value )
  { foo::default_value = new_value; }
  static void 
  change_some_foo(foo * a_foo, int new_value )
  { a_foo -&gt; my_value = new_value; }
};

int foo::default_value = 10;
// must be defined and initialized
//   outside the class
Let's consider the regular case first, of ordinary non-static members. Each instance of the foo class contains an instance of the data member my_value. So if I say
  foo first_foo( 5 );
  foo second_foo( 7 );
  foo third_foo;
</pre>
<p>I now have three instances of foo objects in my program, each
with a different number inside -- 5, 7, and 10. If it helps you
visualise this, draw three boxes with the data values inside and
label them with the names of the instances.</p>
<p>When you define a variable, you are telling the compiler to
reserve a little piece of memory the size of a foo object, and also
telling it the name you will use to manipulate that piece of
memory.</p>
<p>If I now say</p>
<pre class="programlisting">
    first_foo.change_value( 13 );
</pre>
<p>what I am actually doing is invoking some compiler magic.
Because this is a (non-static) member function, it has to operate
on a <span class="bold"><b>specific instance</b></span> of a foo
object, in this case, the one called first_foo. Behind the scenes,
the compiler actually changes my call to something like this:</p>
<pre class="programlisting">
    foo::change_value( &amp;first_foo, 13 );
</pre>
<p>And it rewrites the function up in the class definition to look
like this:</p>
<pre class="programlisting">
void foo::change_value
  ( foo * const this, int new_value )
{ this -&gt; my_value = new_value; }
</pre>
<p>The foo:: before the function call means that this function only
exists within the context of the foo class - it's meaningless to
call change_value() without specifying that it's the function
defined in class foo that I want. (There might be a global function
called change_value() or other classes might have a change_value()
function; all of them can exist in the same program and I call
whichever I need with the scoping mechanism.)</p>
<p>On the other hand, the static function foo::change_some_foo()
does not have a <tt class="varname">this</tt> pointer added to its
parameter list. Often such a function does not refer to any
specific foo object; it might as well be a global function declared
outside the class (but that would pollute the global namespace and
risk name conflicts with functions from other libraries).
Similarly, when you declare a static data member, it means there is
only one instance of that variable in the whole program, and you
refer to it by using the class scope operator (like
foo::default_value) to prevent name clashes.</p>
<p>To get off topic for a minute, in recent years there was an
increasing trend in C++ programming to package up otherwise-global
data and functions into artificial classes, simply to avoid name
conflicts. Imagine that you bought two or three different libraries
and they each contained a global variable called
Library_Version_Number; you couldn't use them together without
changing the vendors' source code. It was this trend that led to
the introduction of namespaces into the language.</p>
<p>Apart from the issue of name conflicts, packaging static data
and functions into a class helps to show that they are related to
the purpose of the class, as with foo::default_value above. It's
better to keep everything together than to create a dependency on a
global variable (if only because it simplifies the documentation
effort.)</p>
<p>Getting back to the static member function change_a_foo(), if it
needs to access a specific foo instance, you have to tell it which
one to look at - hence the a_foo parameter. Because the static
function is a member of the class, it has access to the private
parts of that class. If it were a global function, I would have had
to make it a friend of the class, creating another dependency that
makes it harder to reuse the class.</p>
<p>When it comes to multithreading, there is another reason for
using static functions. You can't tell the thread to run a
particular member function on a particular object, because there is
no way to signal the compiler to add the invisible <tt class=
"varname">this</tt> pointer (remember the underlying systems
threads library is written in C and doesn't understand OO
programming). So I told the thread to run a static member function
and passed it the address of the instance to operate on. If I had
several member functions I might want to invoke, I would have
written several static functions to correspond to them.</p>
<p>Finally, consider the static data member foo::default_value.
There is only one of these in the whole program. If there is any
chance that multiple threads might call set_default_value() at the
same time, or that one of them might change it while some other
thread is reading it, then I would need to protect
foo::default_value with a mutex or some other mechanism.</p>
<p><span class="emphasis"><em>I've another question. It seems that
both change_value() and change_some_foo() can work well.
change_value() gets an object pointer added and passed over to it
implicitly and change_some_foo() gets an object pointer explicitly.
The question arises which one should be used in the class design
?</em></span></p>
<p>You're right - both functions do exactly the same thing. I did
sort of wonder if (1) it was confusing to send them to you that
way, and (2) if I did so, if it would be better design for the
static function to then call the member function on its parameter
instead of tinkering with the data directly. Yes, it would, of
course, but I wanted to show that the static member function did
have access to the private data of an instance.</p>
<p>In general, I think the best design would be to prefer instance
member functions over static functions whenever possible; that fits
better with the OO paradigm of encapsulating data and behaviour
together.</p>
<p>Static functions are best used to bundle some kind of
functionality into the class which is logically related to the
class's purpose, but not part of the behaviour of any instance. An
example of this would be the set_default() function - this is a
value that every instance may have occasion to need, but which
doesn't belong to any particular instance. Now that I think about
it more, here is a better implementation than using a static member
of the class to hold the default value - make it a static variable
within the function itself:</p>
<pre class="programlisting">
class foo:
{
public:
  int default( int new_value =
            SOMETHING_INVALID );
  // other stuff as before
};

int foo::default()
{
  static bool initialized;
  static int default_value;

  if ( !initialized )
  {
    if ( new_value == SOMETHING_INVALID)
        default_value = 10;
    else 
        default_value = new_value;
    initialized = true;
  }
  return default_value;
}
</pre>
<p>When you need the default value to initialise a foo object, you
would get it with the simple call foo::default() - the first time
you do this, if the static variable hadn't been previously
initialised, it would be done at this time. Or you could set it to
a different value with foo::default( 20 ). This of course assumes
that there is some invalid value that could be used to signal that
you just want the existing default.</p>
<p>What are the advantages of this approach?</p>
<p>First, initialization only takes place if you actually need the
default value - if you never ask for it, there's no overhead. If
the initialisation was expensive (maybe going over the network to
get the value?), doing it routinely at the start of every program
would slow things down.</p>
<p>Second, and this is really serious, when multiple object files
are linked together to form a program, the order of initialisation
of static variables is unspecified. So in the previous message,
this line:</p>
<pre class="programlisting">
/*static*/ int foo::default_value = 10;
</pre>
<p>might not be properly initialised when you needed to access it
from another module. This phenomenon is entirely at the whim of the
linker, so it may or may not bite you.</p>
<p>Third, access to the function-static variable is available only
by calling that function. Any class method has access to the static
data members of the class, so more functions need to co-operate
with the locking mechanism, etc.</p>
<p>To get back to your original question, is there any time when
you might want to have a member function and a static function with
basically the same functionality? Occasionally - and here's one
example:</p>
<pre class="programlisting">
class enhanced_string
{
public:
  void uppercase();
  // uppervase my instance data
  static enhanced_string uppercase
    (enhanced_string  const &amp; s)
  {
    enhanced_string temp = s;
    temp.uppercase();
    return temp;
  }
  // other stuff
};
</pre>
<p>So, if you wanted any individual enhanced_string to change
itself to the uppercase representation, you would call its member
function. To acquire a new enhanced_string, which was an uppercased
copy of an existing string, you would call the static function and
pass it the string you wanted to copy. The advantage is that you
don't have to 'fatten' the interface by coming up with two
different names for member functions to perform the different
functionality.</p>
<p>In summary, prefer member functions to static functions if they
make any sense at all.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
