473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

what's wrong with the following singleton class???

// T.h:

class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
};

// T.cpp:

T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();

return smInstance;
}

when I try to compile the above code, there is linker error:
.../T.cpp:3: undefined reference to `T::smInstance`

I am using gcc 3.4.6 under gentoo linux, thanks

Apr 30 '06 #1
10 1907
* yi*************@gmail.com:
// T.h:

class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
};

// T.cpp:

T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();

return smInstance;
}

when I try to compile the above code, there is linker error:
../T.cpp:3: undefined reference to `T::smInstance`

I am using gcc 3.4.6 under gentoo linux, thanks


You have declared but not defined 'smInstance'.

It should be defined in your [T.cpp] file.

But instead, just do

class T
{
private:
T() {}
T( T const& );
~T() {}
public:
static T& instance()
{
T theInstance;
return theInstance;
}
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 30 '06 #2
* Alf P. Steinbach:
* yi*************@gmail.com:
// T.h:

class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
};

// T.cpp:

T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();

return smInstance;
}

when I try to compile the above code, there is linker error:
../T.cpp:3: undefined reference to `T::smInstance`

I am using gcc 3.4.6 under gentoo linux, thanks
You have declared but not defined 'smInstance'.

It should be defined in your [T.cpp] file.

But instead, just do

class T
{
private:
T() {}
T( T const& );
~T() {}
public:
static T& instance()
{
T theInstance;


Should be

static T theInstance;

of course.
return theInstance;
}
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 30 '06 #3
if the class is used in multi-threaded environment, will there be any
problem ????

Apr 30 '06 #4
* yi*************@gmail.com:
if the class is used in multi-threaded environment, will there be any
problem ????


Please quote what you're responding to.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 30 '06 #5
???
When you declare a static member, you should define it in the
implementation.

<yi*************@gmail.com>
??????:11*********************@i39g2000cwa.googleg roups.com...
// T.h:

class T
{
public:
static T* instance();
private:
T() {}
~T() {}
static T* smInstance;
you declared smInstance here (in .h file)
};
but in the implementatin file, (.cpp file), you don't define it (initialize
it). just do like this:

static T::smInstance = NULL;
// T.cpp:

T* T::instance()
{
if (smInstance == NULL)
smInstance = new T();

return smInstance;
}

when I try to compile the above code, there is linker error:
../T.cpp:3: undefined reference to `T::smInstance`

I am using gcc 3.4.6 under gentoo linux, thanks

Apr 30 '06 #6

yi*************@gmail.com wrote:
if the class is used in multi-threaded environment, will there be any
problem ????


Depends on what the singleton does. You always have to lock certain
resources to avoid race conditions and other bugbears.
_Generic_Programming_ has some information you might be interested in.

Apr 30 '06 #7
"ying.gary.zhang" <yi*************@gmail.com> wrote in message
news:11*********************@j73g2000cwa.googlegro ups.com
if the class is used in multi-threaded environment, will there be any
problem ????


yes there would be.
You need to mutex protect access to the critical sections. In this case,
your instance() function should use the "Double checked locking pattern"
for "creation" of the instance.

regards,
Aman Angrish.

--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
May 11 '06 #8

Aman Angrish wrote:
"ying.gary.zhang" <yi*************@gmail.com> wrote in message
news:11*********************@j73g2000cwa.googlegro ups.com
if the class is used in multi-threaded environment, will there be any
problem ????


yes there would be.
You need to mutex protect access to the critical sections. In this case,
your instance() function should use the "Double checked locking pattern"
for "creation" of the instance.

regards,
Aman Angrish.


You only need to do that when it's implemented as a pointer. When it is
implemented as an instance within a function then technically there
should be no race conditions, atlhough you then rely on whether
compilers are compliant enough to implement it that way. Of course, you
can avoid that problem also by ensuring the instance is created before
you create mulitple threads that access it.

Generally I simply don't use singletons. Abandoned them a year or so
ago and never had any real problems.

May 11 '06 #9

Earl Purple wrote:
Generally I simply don't use singletons. Abandoned them a year or so
ago and never had any real problems.


I hear a bell ringing somewhere that singletons are handy for creation
on demand. Something will only be created when required. That said,
singletons open a whole can of worms of its own and one has to read
about 5 books to realize all its subtleties.

If single controlled access to a unique instance, and not "creation on
demand" is the reason for the singleton, I prefer this slight variation
on the singleton:

class Singleton
{
public:
static void create( /*params*/ );
static void destroy();
static Singleton* instance();
//...
};

This seperates responsibilities quite nicely. Create is typically
called only once in the application - right at the beginning. If this
is not the case, instance() throws during first access. Destroy need
never be called, or after the last time Singleton is referenced.
Another idea would be to return a weak_ptr, the instance itself being
the only shared_ptr - destroyed automatically when application ends,
and all references (weak_ptrs) to it invalidated).

Regards,

W

May 11 '06 #10

werasm wrote:
If single controlled access to a unique instance, and not "creation on
demand" is the reason for the singleton, From what I see of the use of singletons, the convenience is being able

to use it without having to pass the reference to it through the class
header, thus supposedly hiding the implementation detail.

I circumvent this by using either the pImpl paradigm or having an
abstract base class and a factory, so users of your class (or its
abstact base) do not need to see how you implement.

With a lot of singletons I come across, although there is only one
instance, there is no particular reason why there couldn't be more than
one - for example a database. Why shouldn't an application interact
with more than one database?

Having an "open the first time on use" does not imply singleton, it
implies an equivalent to lazy-evaluation.

How to handle the re-entrant and mulitple-creation situation is down to
the specifics of the application. If you want multiple connections to a
database each running in different threads, then the best option is to
open the database first just before you create a connection pool. You
ensure this by designing your classes well.

May 11 '06 #11

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

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
24
by: ypjofficial | last post by:
Hello all, I have written a class with many private data members.and i am putting it in a separate dll file. Now when i link that file while writing my main program module,natuarally i have to...
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...
10
by: JoeC | last post by:
I have been programming for a while and I have seen this syntax before and I copied this from a book but the book didn't explain what is going on here. class engine{ protected: static engine*...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.