473,471 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

singleton pattern, return sole pt per reference

its ok to return *this as reference (C++ FAQ L).

but is it also in singleton pattern (ctor example below)?

Timer* Timer::sole(0); // static

Timer& Timer::Get() // static
{
return sole ? *sole : *(sole = new Timer);
}

i think it isnt only if using new (std::nothrow),because of exeption
handling!?

HAND, Chris
Aug 16 '07 #1
3 1675
Hi!

Chris Forone schrieb:
its ok to return *this as reference (C++ FAQ L).
Yes.
but is it also in singleton pattern (ctor example below)?
Yes. Whenever you get a "this" you may return it.
Timer* Timer::sole(0); // static

Timer& Timer::Get() // static
{
return sole ? *sole : *(sole = new Timer);
}
I don't see any "this*" here. And "static" functions do not have a
"this" pointer, anyway. Your code is fine but I don't understand your
question here.

Frank
Aug 16 '07 #2
Frank Birbacher schrieb:
Hi!

Chris Forone schrieb:
>its ok to return *this as reference (C++ FAQ L).

Yes.
>but is it also in singleton pattern (ctor example below)?

Yes. Whenever you get a "this" you may return it.
>Timer* Timer::sole(0); // static

Timer& Timer::Get() // static
{
return sole ? *sole : *(sole = new Timer);
}

I don't see any "this*" here. And "static" functions do not have a
"this" pointer, anyway.
yes, but it returns a reference from a pointer. and if i use "new
(std::nothrow)" it returns a reference from a 0-pointer (bad in C++ FAQ L)

Your code is fine but I don't understand your
question here.
if the code is fine, no more question...
>
Frank
thanks a lot, chris
Aug 16 '07 #3
Hi!

Chris Forone schrieb:
yes, but it returns a reference from a pointer. and if i use "new
(std::nothrow)" it returns a reference from a 0-pointer (bad in C++ FAQ L)
Yeah, right. Bad bad. Hmm, but you could reserve memory in advance:

#include <boost/type_traits.hpp>
using namespace boost;

Foo& getInstance()
{
//use static POD
static bool initialized = false;
static aligned_storage<sizeof(Foo), alignment_of<Foo>
::valuestorage;

if(!initialized)
{
//construct Foo in storage
new (&storage) Foo();
initialized = true;
}

//return instance from storage
return *reinterpret_cast<Foo*>(&storage);
}

HTH,
Frank
Aug 16 '07 #4

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

Similar topics

1
by: Richard A. DeVenezia | last post by:
foo() generates elements with event handlers that invoke foo function properties. Is this an abhorrent or misthought pattern ? It allows just the one occurence of identifier /foo/ to be changed...
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...
13
by: William Stacey | last post by:
FYI. /// <summary> /// Author: William Stacey /// Fast and simple way to implement a singleton pattern without resorting /// to nested classes or other static vodo. Can also be easily converted...
13
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
5
by: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
14
by: rob | last post by:
I have two different classes singleton1 and singleton2 that both are singletons. Both classes implement the same functions. Therefore, I would like to have an interface containing these functions....
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...
10
by: Chris Forone | last post by:
hello group, cant understand the following: Scene* Scene::sole(0); // in Scene.cpp class Scene // singleton in Scene.h { public: static Scene* Get()
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
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,...
1
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.