Logo

dev-resources.site

for different kinds of informations.

C++, Static Object - Singleton pattern

Published at
6/29/2023
Categories
static
singleton
cpp
Author
dinhluanbmt
Categories
3 categories in total
static
open
singleton
open
cpp
open
Author
11 person written this
dinhluanbmt
open
C++, Static Object - Singleton pattern

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;
}
Enter fullscreen mode Exit fullscreen mode

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
Favicon
The Magic of `static` in Java: One for All, and All for One!
Favicon
First AWS Project
Favicon
Astro vs Visual Studio 2022 as Static Site Generators
Favicon
Dynamic vs Static typing (again)
Favicon
A real life example of using Late Static Binding in PHP.
Favicon
Back to the roots: advantages to static web development
Favicon
Choosing Between Static and Non-Static Methods in PHP: Real-World Use Cases and Code Examples
Favicon
C++, Static Object - Singleton pattern
Favicon
C++, Static Object - share data
Favicon
Hosting static sites with Cloudflare R2 and MinIO Client
Favicon
😱 Book Release: Eleventy by Example – Learn 11ty with 5 in-depth projects
Favicon
Host Your Static Website Files for Free with Staticsave: The Fastest and Easiest Way to Share Your Content Online
Favicon
Adding a Table of Contents to dynamic content in 11ty
Favicon
Mix static & client-side rendering on same page with SvelteKit
Favicon
The Top Five Static Site Generators (SSGs) for 2023 β€”Β and when to use them
Favicon
Position-relative and absolute
Favicon
Looking for a TinaCMS or Tina Cloud alternative?
Favicon
Editing Content with Hugo Shortcodes: A Shortcode-Aware CMS
Favicon
How to make use of the GitLab CI for Rust Projects
Favicon
Artisanal Web Development
Favicon
C - Static libraries
Favicon
Simplifying switcheroos
Favicon
What is a Static Site Generator?
Favicon
Static vs Dynamic Websites: The Definitive Guide
Favicon
Ten Myths about Static Websites
Favicon
Dart Typing πŸ’« 🌌 ✨
Favicon
Hosting Static Content with SSL on Azure
Favicon
Watching your Core Web Vitals on Jamstack
Favicon
Automatically update your GitHub Pages website with posts from dev.to
Favicon
Password Protection for Cloudflare Pages

Featured ones: