By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

pimpl idiom and singletons

P: n/a
Some little tidbit I just ran into that might help some, especially
novice programmers.

If you are using the pimpl idiom, as you probably should be most of the
time, then it is very straightforward to turn your class into a
singleton object. Consider:

class X
{
struct impl;
boost::scoped_ptr<implpimpl;

public:
X();
~X();

void f();
}

X::X() : pimpl(new impl){}
X::~X() {}
void X::f() { pimpl->f(); }

To turn that into a singleton just do the following:

1. turn pimpl into a static member.
2. Initialize it in the cpp file as usual.
3. you're done.

You could change it so that the constructor is private and you have an
instance() function but why bother?

X x;
x->f();

Creating the x object is of minimal expense. You may later decide a
singleton isn't a good way to solve the problem. None of your client
code would need to be changed.

There may be some extra bookkeeping to do of course so you might need
some initializer function that is called once. This is pretty easy to
do and can be just built into or called from the constructor.
May 24 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Noah Roberts <us**@example.netwrote in message ...
Some little tidbit I just ran into that might help some, especially
novice programmers.

If you are using the pimpl idiom,
If you are trying to help 'newbies', you might want to tell what 'pimpl' is
so they don't go popping zits.

--
Bob <GR
POVrookie
May 24 '07 #2

P: n/a
BobR wrote:
Noah Roberts <us**@example.netwrote in message ...
>Some little tidbit I just ran into that might help some, especially
novice programmers.

If you are using the pimpl idiom,

If you are trying to help 'newbies', you might want to tell what 'pimpl' is
so they don't go popping zits.
That's easily looked up in google. I can't think of any place where
what I'm describing is on the net though, certainly not as simple as
typing something into a search field.
May 25 '07 #3

P: n/a
On May 24, 3:14 pm, Noah Roberts <u...@example.netwrote:
Some little tidbit I just ran into that might help some, especially
novice programmers.

If you are using the pimpl idiom, as you probably should be most of the
time, then it is very straightforward to turn your class into a
singleton object. Consider:

class X
{
struct impl;
boost::scoped_ptr<implpimpl;

public:
X();
~X();

void f();

}

X::X() : pimpl(new impl){}
X::~X() {}
void X::f() { pimpl->f(); }

To turn that into a singleton just do the following:

1. turn pimpl into a static member.
2. Initialize it in the cpp file as usual.
3. you're done.

You could change it so that the constructor is private and you have an
instance() function but why bother?

X x;
x->f();

Creating the x object is of minimal expense. You may later decide a
singleton isn't a good way to solve the problem. None of your client
code would need to be changed.

There may be some extra bookkeeping to do of course so you might need
some initializer function that is called once. This is pretty easy to
do and can be just built into or called from the constructor.
nonsense! pimpls and singletons are orthogonal
consider the case of the 'impl' having data/state

Diego

May 25 '07 #4

P: n/a
Diego Martins wrote:
nonsense! pimpls and singletons are orthogonal
consider the case of the 'impl' having data/state
What makes you think that singletons don't have those things??
May 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.