473,406 Members | 2,894 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.

lightweight singleton template using references

Hi,
apologies for yet another Singleton posting.

From reading around previous postings etc, I've cobbled together two
Singleton template definitions - one that returns a reference, and one
that returns a pointer. I understand the pointer one and it works,
but the reference one doesn't work as I expect. Can anybody explain
this to me? (Code below).

I'm not really looking for thread safety or for something as general
as in Loki. Really am just trying to figure out what's going on.

The error is that two subsequent calls to
Singleton<myType>::Instance() seem to return references to two
different objects, though confusingly, the myType constructor is
called only once (and the myType destructor called 3 times, and the
Singleton destructor not at all):

// CODE OUTPUT:

unix> ./a.out
Singleton<T>::Instance():
myvec constructor:
10 bffff610 <--- first singleton
Singleton<T>::Instance():
0 bffff648 <--- second singleton - different size and
address!
myvec destructor:
myvec destructor:
myvec destructor:
unix>

// COMPLETE SOURCE CODE:
// attempt to have a Singleton template which returns
// a reference.
#include <iostream>
#include <vector>
using namespace std;

//----------------------------------
// Singleton template that returns a reference:

template< class T >
class Singleton : public T
{
private:
Singleton(){cout << "Singleton constructor:";}
Singleton( const Singleton& );
~Singleton(){ cout << "Singleton destructor:";}
public:
static T& Instance(){
cout << "Singleton<T>::Instance():"<<endl;
static T t;
return t;
}
};

//----------------------------------
// just a dumb class to use as the template class T
// in the Singleton<T>:
class myvec
{
private:
int len;

public:
vector<int> v;

myvec(){ cout << "myvec constructor: "<<endl;}
~myvec(){ cout << "myvec destructor: " << endl;}
myvec( const myvec& rhs ): v(rhs.v), len(rhs.len) {}
myvec& operator=(const myvec& rhs){ v=rhs.v; len=rhs.len; return
*this; }

void init( int N ){ v.resize(N); len=N; }
void resize( int N ){ v.resize(N); len=N; }
int size(){ return v.size();}

};
//----------------------------------
// Main:
int main(){

// first instance:

myvec V = Singleton<myvec>::Instance();
V.init( 10 );

cout << V.size() << " " << &V << endl;

// second instance:

myvec W = Singleton<myvec>::Instance();

cout << W.size() << " " << &W << endl;

// NB W.size()=0 while V.size() = 10!
// also W and V have different addresses!
}

Thanks for any comments,
Sean Dettrick
Jul 19 '05 #1
2 5831
Sean Dettrick wrote:
Hi,
apologies for yet another Singleton posting.

//----------------------------------
// Main:
int main(){

// first instance:

myvec V = Singleton<myvec>::Instance();
You probably meant this:

myvec & V = Singleton<myvec>::Instance();

V.init( 10 );

cout << V.size() << " " << &V << endl;

// second instance:

myvec W = Singleton<myvec>::Instance();


myvec & W = Singleton<myvec>::Instance();
myvec V declares a myvec V.

I don't think this creates the singleton you were thinking.

Jul 19 '05 #2
Gianni Mariani <gi*******@mariani.ws> wrote in message news:<bk********@dispatch.concentric.net>...
Sean Dettrick wrote:
Hi,
apologies for yet another Singleton posting.


//----------------------------------
// Main:
int main(){

// first instance:

myvec V = Singleton<myvec>::Instance();


You probably meant this:

myvec & V = Singleton<myvec>::Instance();

Thank you! That solved it.

And now if I make the myvec constructors and destructors private, and
declare Singleton<myvec> as its friend, then I seem to be protected
against the creation of any other myvecs (assuming single threading).
Any other comments?

class myvec
{
friend class Singleton<myvec>;
private:
int len;

myvec(){ cout << "myvec constructor: "<<endl;}
~myvec(){ cout << "myvec destructor: " << endl;}
myvec( const myvec& rhs ): v(rhs.v), len(rhs.len) {}
myvec& operator=(const myvec& rhs){ v=rhs.v; len=rhs.len; return
*this; }
public:
vector<int> v;

void init( int N ){ v.resize(N); len=N; }
void resize( int N ){ v.resize(N); len=N; }
int size(){ return v.size();}

};
Jul 19 '05 #3

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

Similar topics

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)...
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 ...
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...
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
3
by: Raider | last post by:
I need to have one object for each template argument(s) used. For example, I need to have one object of int. I tried the following code and it gives me all I want with Visual C++ 7.1. But is it...
15
by: Nick Keighley | last post by:
Hi, I found this in code I was maintaining template <class SingletonClass> SingletonClass* Singleton<SingletonClass>::instance () { static SingletonClass _instance; return &_instance; }
5
by: Damien | last post by:
Hi all, I'm using a pretty standard C++ Singleton class, as below: template <typename T> class Singleton { public: static T* Instance() {
2
by: ben chang | last post by:
i'm hacking at some code originally written for VC++, trying to port to linux GCC 4. the linker returns multiple-definition errors with a singleton object, which i guess is not a problem in visual...
2
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.