How to do a singleton pattern with lazy instantiation?

D

Damon

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
 
G

Gianni Mariani

Damon said:
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.
 
C

Chris Newton

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
 
M

Martijn Lievaart

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top