mockers
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Singleton class - only a single object

5 posters

Go down

Singleton class - only a single object Empty Singleton class - only a single object

Post  ramthegreatcv Sun Feb 01, 2009 11:56 pm

make a class in c++ such that we can make only one instance of the class ( we can make only a single object )

-contributed by rohit

possible solution:
class
{
int a;
}obj1;
ramthegreatcv
ramthegreatcv

Posts : 55
Join date : 2009-01-30
Age : 36

Back to top Go down

Singleton class - only a single object Empty @ramu

Post  mnnit.rahul Mon Feb 02, 2009 2:32 am

your solution is wrong we can create second object also by giving its name there
your solution should be such that if someone tries to declare another instance of this class then that should give an error message

mnnit.rahul

Posts : 51
Join date : 2009-02-01

Back to top Go down

Singleton class - only a single object Empty Re: Singleton class - only a single object

Post  Admin Mon Feb 02, 2009 2:47 am

how about using some static counter...and checking in constructor itself?
please put the ans,if u know.

Admin
Admin

Posts : 47
Join date : 2009-01-29

https://mockers.forumotion.com

Back to top Go down

Singleton class - only a single object Empty @ramu

Post  mnnit.rahul Tue Feb 03, 2009 3:24 am

yes u are right we have to use either static variable inside class or a global variable

first try to make a object of a class which has default constructor as private and no constructor as public

mnnit.rahul

Posts : 51
Join date : 2009-02-01

Back to top Go down

Singleton class - only a single object Empty privte constructor

Post  obama Tue Feb 03, 2009 4:58 pm

#include<iostream>
using namespace std;
class a
{

a(){//some code
}
a(a &);
public:
static a dube;
}
a::dube;
main()
{

cout<<"hahahha";
}
cheers

obama

Posts : 4
Join date : 2009-02-03

Back to top Go down

Singleton class - only a single object Empty Re: Singleton class - only a single object

Post  mnnit.rahul Tue Feb 03, 2009 5:39 pm

yes this will create a single object but the object is created inside class and cannot be used in main without scope resolution operator along with object.
there exist another solution also

waise kaun ho bhai obama bahut tagda solution tha yaar ye

mnnit.rahul

Posts : 51
Join date : 2009-02-01

Back to top Go down

Singleton class - only a single object Empty I think this can also be a good solution

Post  rohit vashishtha Wed Feb 04, 2009 4:28 pm

class sample
{
static sample *ptr ;
private:
sample( )
{
}
public:
static sample* create( )
{
if ( ptr == NULL )
ptr = new sample ;
return ptr ;
}
} ;
sample *sample::ptr = NULL ;
void main( )
{
sample *a = sample::create( ) ;
sample *b = sample::create( ) ;
}

rohit vashishtha

Posts : 2
Join date : 2009-02-01

Back to top Go down

Singleton class - only a single object Empty Re: Singleton class - only a single object

Post  mnnit.rahul Sun Mar 01, 2009 7:11 pm

are rohit bhai aapka solution kabhi galat ho sakta hai

mnnit.rahul

Posts : 51
Join date : 2009-02-01

Back to top Go down

Singleton class - only a single object Empty Re: Singleton class - only a single object

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum