Boost logo

Boost :

From: Rene Rivera (grafik666_at_[hidden])
Date: 2002-08-06 20:28:49


[2002-08-07] Andrew J Bromage wrote:

>G'day all.
>
>In retrospect I shouldn't have brought up a "more powerful"
>call_once function, because that's not what I want at all. What
>I "really" want is this:
>
> class once
> {
> public:
> once();
> ~once();
> call_once(function0<void>&);
> };
>
>On Tue, Aug 06, 2002 at 09:13:26AM -0400, Pete Becker wrote:
>
>> The one place where they seem useful is in lazy initialization. But
rather
>> than thinking in C terms and prolonging the life of the inside-out
>> interface to once functions, think C++ and design a more suitable
solution:
>>
>> class lazy_initializer
>> {
>> public:
>> lazy_initializer();
>> ~lazy_initializer();
>> void init_once();
>> private:
>> virtual void do_init() = 0;
>> };
>
>This is in fact exactly what I wanted to use the once operation for,
>except that my class needs more than one piece of data which is lazily
>evaluated. Admittedly composing with more than one lazy_initializer
>class would work, but it seems less clean than just dropping a few once
>objects into the class.

Hmmm, maybe I missed something but... If it's singleton type data why not
make it static? Or is making it static the problem? If it is I tend to use
this solution for static class lazy init data...

class something
{
    public:
    something();
    void print() { cout << data(); }
    int get() { return data(); }
    void set(int aValue) { data() = aValue; }
    
    protected:
    int & data()
    {
        static int sData = 10;
        return sData;
    }
};

Is this what you really want?

-- grafik - Don't Assume Anything
-- rrivera_at_[hidden] - grafik_at_[hidden]
-- 102708583_at_icq - Grafik666_at_AIM - Grafik_at_[hidden]


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