How to do a singleton pattern with lazy instantiation?

Discussion in 'C++' started by Damon, Jan 23, 2004.

  1. Damon

    Damon Guest

    Hi,

    I tried to perform a singleton pattern with lazy instantiation but
    keep getting linker errors. Could someone advise what I'm doing
    wrongly? I'm using gcc version 3.2.2. Thanks in advance.

    --------------linker errors-------------
    eve.o(.text+0x366): In function `main':
    /usr/include/c++/3.2.2/i386-slackware-linux/bits/atomicity.h:50:
    undefined reference to `eve::sq_note::sq_note[in-charge]()'
    collect2: ld returned 1 exit status
    make: *** [eve] Error 1

    ---------------basic_note.h------------
    #ifndef BASIC_NOTE_H
    #define BASIC_NOTE_H

    #include "base/object.h"

    namespace eve {
    class basic_note : public object {
    protected:
    basic_note() {}

    public:
    virtual ~basic_note() {}


    };
    };

    #endif

    ---------------sq_note.h---------------
    #ifndef SQ_NOTE_H
    #define SQ_NOTE_H

    #include "basic_note.h"

    namespace eve {
    class sq_note : public basic_note {
    private:
    static sq_note* singleton;

    public:
    sq_note();

    ~sq_note() {
    delete singleton;
    }

    static sq_note *instance() {
    if( NULL==singleton ) {
    singleton = new sq_note(); //problem with this line!
    }
    return singleton;
    }

    };

    sq_note *sq_note::singleton = NULL;

    };

    #endif

    Regards,
    Damon
    Damon, Jan 23, 2004
    #1
    1. Advertising

  2. Damon wrote:
    > Hi,
    >
    > I tried to perform a singleton pattern with lazy instantiation but
    > keep getting linker errors. Could someone advise what I'm doing
    > wrongly? I'm using gcc version 3.2.2. Thanks in advance.


    You don't provide a constructor ...

    ....

    >
    > ---------------sq_note.h---------------
    > #ifndef SQ_NOTE_H
    > #define SQ_NOTE_H
    >
    > #include "basic_note.h"
    >
    > namespace eve {
    > class sq_note : public basic_note {
    > private:
    > static sq_note* singleton;
    >
    > public:
    > sq_note();


    This says that there is no way to construct and object of type
    eve::sq_note() unless you provide a definition of it in some other
    compilation unit.

    >
    > ~sq_note() {
    > delete singleton;
    > }
    >
    Gianni Mariani, Jan 23, 2004
    #2
    1. Advertising

  3. Damon

    Damon Guest

    Thank you, Gianni. I can't believe I am so dense! Need to change my eyeballs. ;-P

    Gianni Mariani <> wrote in message news:<buqisl$>...
    > Damon wrote:
    > > Hi,
    > >
    > > I tried to perform a singleton pattern with lazy instantiation but
    > > keep getting linker errors. Could someone advise what I'm doing
    > > wrongly? I'm using gcc version 3.2.2. Thanks in advance.

    >
    > You don't provide a constructor ...
    >
    > ...
    >
    Damon, Jan 23, 2004
    #3
  4. Damon

    Chris Newton Guest

    Damon wrote...
    > I tried to perform a singleton pattern with lazy instantiation but
    > keep getting linker errors. Could someone advise what I'm doing
    > wrongly?


    You're using a pointer. :)

    Seriously, unless you need to control the exact time of destruction or
    want some other more precise behaviour, it's often easier just to do
    something like this...

    (NB: Untested code, beware silly mistakes.)

    class Singleton
    {
    public:
    static Singleton& Instance();

    private:
    Singleton();
    };

    Singleton& Singleton::Instance()
    {
    static Singleton theOnlyOne;
    return theOnlyOne;
    }

    The static object in the Instance() member function won't be constructed
    until the first time you call that function, or at all if you never call
    the function. If you do call Instance(), the static object will also be
    destroyed automatically when you program ends, unless you do something
    daft to prevent it, and I have no idea why you would.

    HTH,
    Chris
    Chris Newton, Jan 24, 2004
    #4
  5. On Sat, 24 Jan 2004 03:06:25 +0000, Chris Newton wrote:

    > The static object in the Instance() member function won't be constructed
    > until the first time you call that function, or at all if you never call
    > the function. If you do call Instance(), the static object will also be
    > destroyed automatically when you program ends, unless you do something
    > daft to prevent it, and I have no idea why you would.


    Also note that C++ guarentees that these statics are destructed in the
    reverse order they are constructed. Which is a mighty good guarentee to
    have!

    HTH,
    M4
    Martijn Lievaart, Jan 24, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Pu
    Replies:
    3
    Views:
    661
    Steven D'Aprano
    Jan 16, 2009
  2. Boris Borcic
    Replies:
    0
    Views:
    540
    Boris Borcic
    Jan 16, 2009
  3. Boris Borcic
    Replies:
    0
    Views:
    538
    Boris Borcic
    Jan 16, 2009
  4. Wilhelm
    Replies:
    1
    Views:
    153
  5. Rick DeNatale
    Replies:
    1
    Views:
    102
    Rick DeNatale
    Oct 25, 2007
Loading...

Share This Page