0% found this document useful (0 votes)
88 views4 pages

C++ Singleton

The Singleton class ensures that only one instance of the class can be created. It defines a GetInstance method that clients use to access the single instance. The constructor is private to prevent direct construction, and GetInstance checks if an instance exists already before creating a new one, thus guaranteeing only one instance across the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views4 pages

C++ Singleton

The Singleton class ensures that only one instance of the class can be created. It defines a GetInstance method that clients use to access the single instance. The constructor is private to prevent direct construction, and GetInstance checks if an instance exists already before creating a new one, thus guaranteeing only one instance across the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

/**

* The Singleton class defines the `GetInstance` method that serves as an

* alternative to constructor and lets clients access the same instance of this

* class over and over.

*/

class Singleton

/**

* The Singleton's constructor should always be private to prevent direct

* construction calls with the `new` operator.

*/

protected:

Singleton(const std::string value): value_(value)

static Singleton* singleton_;

std::string value_;

public:

/**

* Singletons should not be cloneable.

*/

Singleton(Singleton &other) = delete;

/**

* Singletons should not be assignable.

*/
void operator=(const Singleton &) = delete;

/**

* This is the static method that controls the access to the singleton

* instance. On the first run, it creates a singleton object and places it

* into the static field. On subsequent runs, it returns the client existing

* object stored in the static field.

*/

static Singleton *GetInstance(const std::string& value);

/**

* Finally, any singleton should define some business logic, which can be

* executed on its instance.

*/

void SomeBusinessLogic()

// ...

std::string value() const{

return value_;

};

Singleton* Singleton::singleton_= nullptr;;

/**

* Static methods should be defined outside the class.

*/

Singleton *Singleton::GetInstance(const std::string& value)

/**
* This is a safer way to create an instance. instance = new Singleton is

* dangeruous in case two instance threads wants to access at the same time

*/

if(singleton_==nullptr){

singleton_ = new Singleton(value);

return singleton_;

void ThreadFoo(){

// Following code emulates slow initialization.

std::this_thread::sleep_for(std::chrono::milliseconds(1000));

Singleton* singleton = Singleton::GetInstance("FOO");

std::cout << singleton->value() << "\n";

void ThreadBar(){

// Following code emulates slow initialization.

std::this_thread::sleep_for(std::chrono::milliseconds(1000));

Singleton* singleton = Singleton::GetInstance("BAR");

std::cout << singleton->value() << "\n";

int main()

std::cout <<"If you see the same value, then singleton was reused (yay!\n" <<

"If you see different values, then 2 singletons were created (booo!!)\n\n" <<

"RESULT:\n";

std::thread t1(ThreadFoo);

std::thread t2(ThreadBar);
t1.join();

t2.join();

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy