dev-resources.site
for different kinds of informations.
C++, Static Object - Singleton pattern
Published at
6/29/2023
Categories
static
singleton
cpp
Author
dinhluanbmt
Author
11 person written this
dinhluanbmt
open
For many reasons, we may want only one instance of a class to exist throughout the lifetime of an application. This is where the Singleton pattern comes
class mSingleObj {
private:
static shared_ptr<mSingleObj> instance;
string description = "this is single instance object";
// private constructor so that we can not create an instance
mSingleObj(){}
// private copy constructor and assignment operator
// previous version of c++
mSingleObj(const mSingleObj&){}
mSingleObj& operator=(const mSingleObj&) {}
// move constructor and move assignment operator
// >= c++11 we can delete
mSingleObj(mSingleObj&& ) = delete;
mSingleObj& operator=(mSingleObj&& ) = delete;
public:
//the only one way to to get object
static shared_ptr<mSingleObj> getInstance() {
return instance;
}
void display() {
cout << description << endl;
}
~mSingleObj() { cout << "Destructor called only one..." << endl; }
};
//static object need to be initialized outside the class defination
shared_ptr<mSingleObj> mSingleObj::instance(new mSingleObj());
void testSingletonObj() {
shared_ptr<mSingleObj> sObj = mSingleObj::getInstance();
sObj->display();
shared_ptr<mSingleObj> other_sObj = mSingleObj::getInstance();
other_sObj->display();
//check whether objects are the same
cout << "(sObj) Address of managed object: " << sObj.get() << std::endl;
cout << "(other_sObj) Address of managed object: " << other_sObj.get() << std::endl;
}
Important things:
- the private constructor
- a way to get the object (because we cant not create an object of class with private constructor)
- the private Copy constructor and Copy assignment operator
But in my experience, I really dislike the Singleton pattern when dealing with unit tests.
static Article's
30 articles in total
The Magic of `static` in Java: One for All, and All for One!
read article
First AWS Project
read article
Astro vs Visual Studio 2022 as Static Site Generators
read article
Dynamic vs Static typing (again)
read article
A real life example of using Late Static Binding in PHP.
read article
Back to the roots: advantages to static web development
read article
Choosing Between Static and Non-Static Methods in PHP: Real-World Use Cases and Code Examples
read article
C++, Static Object - Singleton pattern
currently reading
C++, Static Object - share data
read article
Hosting static sites with Cloudflare R2 and MinIO Client
read article
π± Book Release: Eleventy by Example β Learn 11ty with 5 in-depth projects
read article
Host Your Static Website Files for Free with Staticsave: The Fastest and Easiest Way to Share Your Content Online
read article
Adding a Table of Contents to dynamic content in 11ty
read article
Mix static & client-side rendering on same page with SvelteKit
read article
The Top Five Static Site Generators (SSGs) for 2023 βΒ and when to use them
read article
Position-relative and absolute
read article
Looking for a TinaCMS or Tina Cloud alternative?
read article
Editing Content with Hugo Shortcodes: A Shortcode-Aware CMS
read article
How to make use of the GitLab CI for Rust Projects
read article
Artisanal Web Development
read article
C - Static libraries
read article
Simplifying switcheroos
read article
What is a Static Site Generator?
read article
Static vs Dynamic Websites: The Definitive Guide
read article
Ten Myths about Static Websites
read article
Dart Typing π« π β¨
read article
Hosting Static Content with SSL on Azure
read article
Watching your Core Web Vitals on Jamstack
read article
Automatically update your GitHub Pages website with posts from dev.to
read article
Password Protection for Cloudflare Pages
read article
Featured ones: