472,958 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1663
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: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.