473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to do a singleton pattern with lazy instantiation?

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
Jul 22 '05 #1
4 2868
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;
}


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

Gianni Mariani <gi*******@mariani.ws> wrote in message news:<bu********@dispatch.concentric.net>...
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 ...

...

Jul 22 '05 #3
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

Jul 22 '05 #4
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

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
18
by: David | last post by:
Hello, I'm writing a C# class library that may be called by non-.Net applications. The main class of this class libray is a ServicedComponent so that non-.Net application can call this class as a...
7
by: phl | last post by:
hello, My project in a web project. I choose to use singleton in one of my projects. Towards the end I realise that I seemed to have refered to the fields singleton in my other classes in my...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
56
by: Luke Matuszewski | last post by:
I am designing the library, which will hidden all its functions within singleton object ... So for clients they will use it like . eg. system.getElementWithId('ruler'); At library side, i...
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
5
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.