- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Edit Admin
That's got to work surely? I'll only get one in my program?
Admin
Obviously, the way to create singletons is to use
pthread_once
.Edit Admin
:%s/design pattern/bad programming/g
Edit Admin
Most (but not all) uses of the singleton pattern are bad programming. It is almost always better to manage the lifecycle of a single instance through more conventional methods- like just creating one instance of it.
Edit Admin
Whether typo or eggcorn, that's really poetic! The more conventional phrase would be "where oh where".
Edit Admin
What that will do is create a separate Frist instance for each compilation unit (.cpp file), not visible from other compilation units. To create a true program-wide singleton, you need to declare it as
extern
in the .h file, and have exactly one .cpp file defining the instance.