Tuesday, 3 April 2012

Singleton without code

Class ABC
{
int a;
int b;
static int c;
}ABC;

ABC::c=10;

void fun()
{
cout<<" Value of a is "<<ABC.a<<endl;
}

No need to write logic to handle singleton, this was you'll have only one object in the program ABC.

Example:

#include<stdio.h>
class singleton
{
    public:
static int value;
    void print()
    {
        printf("hello %d\n",value);
    }
    int a;
}singleton;
singleton::value=5;

int main()
{
//    singleton s;  #cannot define a new instant like this anymore.
    singleton.a=5;
    printf("a=%d\n",singleton.a);
    singleton.print();
    return 0;
}

2 comments:

  1. Good one. Please remove the commented code, so that newebies will not get confused.

    ReplyDelete

AWS Data Pipeline Services

https://www.youtube.com/watch?v=tykcCf-Zz1M