[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Give me a best example for a sigleton class usage

Posted on 2000-03-03
5
Medium Priority
?
232 Views
Last Modified: 2012-05-04
A singleton class is  a class which is having only one object at any time, pls give where it can be useful and how internally it matains .
0
Comment
Question by:madhusrj
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
KangaRoo earned 150 total points
ID: 2579568
If it represents something of which there can be only one, for instance an object representing a printer, or an application object.
Singleton objects are often 'maintained' as static class members

class Singleton
{
    private:
        static Singleton the_object;
        // ...
    public:
       static Singleton& GetIt() { return the_object; }
       // .....
};
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2579573
An alternative implementation for GetIt:

Singleton& SingleTon::GetIt()
{
     static Singleton the_object;
     return the_object;
}

This has the advantage that it will only be instantiated if it is actually used. It also asures you that it will exist when used.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2579632
That seems like an answer.

one additional point.  The singleton should have a private constructor and destructor, this prevents the user from creating additional instances of the singleton.  

Also there are other variations in the way to impliment signletons, but that is the basic idea.
0
 
LVL 1

Expert Comment

by:mjswart
ID: 2646290
How would a client call it then?

Singleton *s = Singleton::GetIt();
doesn't work.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2646484
That version doesn't return a pointer, it returns a reference. so it wouldd be

Singleton &s = Singleton::GetIt();

(If it returned a pointer, which could be done) your code would have worked.)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question