Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Give me a best example for a sigleton class usage

Posted on 2000-03-03
5
Medium Priority
?
228 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

609 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