469,326 Members | 1,331 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Singleton class - by overloading "new" operator

Singleton class - is a class of which only one object exists in memory at a time.
We can create singleton class using :
  • A static method or
  • By overloading "new" operator for that class.
Here is a simple example of creating singleton class by overloading "new" operator for that class.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<alloc.h>
  5. class iSingleton
  6. {
  7.  static iSingleton *sptr;    //this pointer points to object of the class
  8.  public:
  9.     iSingleton()    //public constructor
  10.     {
  11.        sptr=this;
  12.     }
  14.  void *operator new(size_t);    //overload new operator for class which is to be made iSingleton.
  15. };
  17.  iSingleton *iSingleton::sptr=NULL;    //initialize pointer to NULL
  19. void *iSingleton::operator new(size_t s)
  20. {
  21.   if(sptr!=NULL)    //if already one object is created return reference to same object
  22.      return sptr;
  23.   else
  24.      return malloc(s);    //else allocate memory for one (first) object
  25. }
  27. int main()
  28. {
  29.  clrscr();
  30.  iSingleton *sptr1=new iSingleton;      //first object created
  31.  iSingleton *sptr2=new iSingleton;      //second object created
  33.  if(sptr1==sptr2)
  34.       cout<<"\nGiven class is a Singleton class.";
  35.  else
  36.       cout<<"\nGiven class is not a Singleton class.";
  38.  getch();
  39.  return 0;
  40. }
Attached Files
File Type: txt iSingleton.txt (1.0 KB, 495 views)
Aug 8 '12 #1
7 36396
Wow, very interesting and useful. I'm currently working on my first videogame (it's a for fun project) and was wrapping my head around how could I use the least amount of memory when creating objects like floor blocks in a 2D game. This information could prove useful. Say, if I have a block on the screen that is a singleton class in memory, does that mean that if I want its texture in multiple locations on the screen I cant simply change the rectangle in the class because it is only one object in memory? In other words, can I replicate the texture without creating copies of the the class? I'm thinking of a platform type of game. Also, what situations are singleton classes most often used in the professional world?
Aug 9 '12 #2
I think Singleton class is useful in situation where only one instance of the class is to be reused throughout the application.
  • It provides global access to the single instance of the class and
  • It guarantees that no more than one instance of that type can ever be created.
I don't know about 2D game programming so can't say about it..
Aug 10 '12 #3
gr8, very interesting and useful. this code is very optimize for singleton design pattern.
Aug 14 '12 #4
9,208 Expert Mod 8TB
Did anyone read: http://bytes.com/topic/c/insights/65...erns-singleton?
Jan 8 '13 #5
Nice thought, just a suggestion:
It works fine till the objects are created on heap using operator new.

Since the default constructor is still public, multiple number of stack objects can be created by writing:
iSingleton ob1;
iSingleton ob2;//.. and so on

What you can do to avoid this is to declare the destructor private. That way the code won't even compile if someone dares to declare automatic( or even static ) objects( since declaring an automatic objects implicitly means calling the destructor at some later point).

The only way left to create objects then will be to use operator new.
Jun 23 '14 #6
9,208 Expert Mod 8TB
It's not that there is one singleton object. You cannot prevent someone creating an object either on the stack or the heap.

The key is that there is a universal point of access where only one object is referenced. That is the singleton object.

To find the correct object, you use the Instance() method of the object. So if you have 25 objects of the Singleton class you will find that the Instance() method of these objects always returns the same address. That address is the official singleton.
Jun 23 '14 #7
Thanks SumitDhyani for your reply.
Jun 23 '14 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Vijayaraghavan Kalyanapasupathy | last post: by
18 posts views Thread by Leslaw Bieniasz | last post: by
5 posts views Thread by Mark P | last post: by
1 post views Thread by sven_c_t | last post: by
5 posts views Thread by Sam | last post: by
14 posts views Thread by mlw | last post: by
36 posts views Thread by Pat | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.