Solved

Give me a best example for a sigleton class usage

Posted on 2000-03-03
5
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
KangaRoo earned 50 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

724 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