Boost logo

Boost :

From: j.adelman_at_[hidden]
Date: 2002-08-12 05:35:28


Quoting Ashot Nakashian <AshotN_at_[hidden]>:
 
> Hi,
>
>
>
> Static objects in functions are very handy for
recursion and for
> lazy-construction of the objects. It's
the "create once (on demand)
> use
> many" mechanism, from the users perspective.
But sometimes the object
> in
> question is expensive to keep in memory when it
is no longer needed.
>
>
>
> Cases where certain functions are called rarely
and when they are
> done,
> they *know* they don't need the static object
(s) they created,
> probably
> until the function is invoked again. I'm
assuming that the function
> needs static object(s) for its internal
use/algorithm, and it will
> utilize them only when it calls itself, but
expects the static object
> creation mechanism to kick in when the static
object(s) are not
> created.

For recursion, it's surely far more sane to use
something like the following:

struct factorial
{
  int current,max,total; // Imagine this is some
*big* data

  operator int(){return total;}

  factorial(int x):current(1),max(x),total(1){}

  void recurse()
  {
    total*=current++;
    if(current<=max) recurse();
    return;
  }
}

int main()
{
  return factorial(5);
}

-------------------------------------------------
This mail sent through UK Online webmail


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk