?
Solved

Give me a best example for a sigleton class usage

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

Industry Leaders: 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 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.
Suggested Courses

752 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