pimpl idiom and singletons

N

Noah Roberts

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<impl> pimpl;

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.
 
B

BobR

Noah Roberts said:
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.
 
N

Noah Roberts

BobR said:
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.
 
D

Diego Martins

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<impl> pimpl;

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
 
N

Noah Roberts

Diego said:
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??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top