answersLogoWhite

0

What is singleton in c plus plus?

Updated: 12/16/2022
User Avatar

Wiki User

12y ago

Best Answer

A singleton is a class of object from which only one instance of the object can be instantiated within your program. They are generally used to store global variables or to provide some common functionality, such as an application log. They are generally frowned upon because of their global nature, but when you need common functionality there are far worse ways of going about it than with a singleton.

There are several design patterns for a singleton, but probably the simplest and most common form is shown below. Note the private constructors and assignment operator, and the static member method containing a static variable -- the only instance of the class that can ever exist.

Access to the members (not shown) is via a static call:

CSingleton::Instance().<member>

class CSingleton

{

private:

CSingleton() {}; // Prevent construction outside of the class.

CSingleton(const CSingleton&); // Prevent copy-construction.

CSingleton& operator=(const CSingleton&); // Prevent assignment.

public:

static CSingleton& Instance()

{

static CSingleton singleton;

// Created on first access.

// Destroyed on application exit.

return( singleton );

}

// Specific members omitted. Dependant upon purpose of singleton.

};

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is singleton in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you declare a method within a method in c or c plus plus?

C: there are no methods in C. C++: no.


What is the different of c and c plus plus?

c is procedure oriented and c++ is object oriented &amp; much newer.


How A plus B plus C plus D plus 80 plus 90 equal to 100 what is the value of A B C and D?

If a + b + c + d + 80 + 90 = 100, then a + b + c + d = -70.


In computer language C plus plus is related to?

C++ is related to C, the language from which it is derived.


What is meant by println in c plus plus?

println is not a C++ keyword.

Related questions

What do you call the combination of variables and functions into a single object in C plus plus?

A singleton.


What has the author Ira C Singleton written?

Ira C. Singleton has written: 'Music in secondary schools' -- subject(s): Instruction and study, School music, United States


What has the author Ross C Singleton written?

Ross C. Singleton has written: 'Industrial organization and antitrust' -- subject(s): Antitrust law, Economic aspects of Antitrust law, Industrial organization


What is b plus b plus b plus c plus c plus c plus c?

b+b+b+c+c+c+c =3b+4c


What is c plus c plus 2c plus c plus c equal?

c + c + 2c + c + c = 6c


B plus b plus b plus c plus c plus c plus c equals?

b + b + b + c + c + c + c = 3b + 4c


Symplify c plus c plus c plus c?

4c


What is c plus c plus c plus c plus c?

c + c + c + c + c = 5 * c.


Primary and secondary key in c and c plus plus?

There are no "primary and secondary keys" in c and c plus plus.


What is c plus C plus C answer in algebra?

3c


What is the birth name of IronE Singleton?

IronE Singleton's birth name is Robert Singleton.


What is the birth name of Doris Singleton?

Doris Singleton's birth name is Dorthea Singleton.