Solved

Give me a best example for a sigleton class usage

Posted on 2000-03-03
5
199 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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
How would a client call it then?

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

Expert Comment

by:nietod
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now