473,386 Members | 1,621 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,386 software developers and data experts.

pimpl idiom and singletons

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
4 3791

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Icosahedron | last post by:
I've been going through some old code trying to clean it up and rearchitect it based on more modern C++ idioms. In the old code I often used the Pimpl idiom on a class by class basis, creating...
6
by: Asfand Yar Qazi | last post by:
Hi, Now that GCC 3.4 has precompiled headers, I'm thinking I can stop using pimpls to speed up development time, as it may make life easier (declaring pimpls takes a long time...) What are...
2
by: Debajit Adhikary | last post by:
I'm still pretty new to design patterns... I was wondering, is there any difference between the Bridge Pattern and Herb Sutter's Pimpl Idiom? Both delegate responsibility to an implementation...
2
by: Peteris Krumins | last post by:
Hello! I was playing around pimpl idiom and discovered that there is a problem with it if a class member template function exists which has to access private data, since only the forward...
9
by: Edward Diener | last post by:
Because 'friend' is not recognized in MC++, using the pImpl idiom in MC++ classes seems nearly impossible. Normally a pImpl class is a 'friend' to the class for which it supplies the private...
10
by: red floyd | last post by:
It seems that the use of auto_ptr<> is discouraged in many places in favor of boost::shared_ptr<> (or tr1::shared_ptr<>). But consider a PIMPL idiom, where there is a strict 1-1 relationship...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
14
by: Daniel Lidström | last post by:
Hello! I have just discovered a way to use the private implementation idiom (pimpl), without the overhead of dynamic memory allocation. For those of you who don't know what this is, Wikipedia...
2
by: Graham Reitz | last post by:
What are good strategies for selecting, either at run-time or compile time, various pimpl'ed implementations? While retaining the ability to switch implementations without recompiling. Boost...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.