saumendra
asked on
class design
design a class so that ,only one instance of that class can be made. no second instance is possible.
Yes you can have only a single instance of a class which is called a Singleton class.
A singleton class is one in which you keep the constructor as a private method. The constructor can be a default constructor.
A singleton class is one in which you keep the constructor as a private method. The constructor can be a default constructor.
I agree with sateesh_babu.
Example:
class Singleton {
static Singleton single;
int i;
Singleton(int I) : i(I) {}
public:
static Singleton* instance() {return &single;}
int val() { return i; }
};
Singleton Singleton::single(11);
main() {
//Singleton x(1); // error can't create a Singleton
// it is possible to access the single instance
cout << Singleton::instance()->val () << endl;
return 0;
}
Example:
class Singleton {
static Singleton single;
int i;
Singleton(int I) : i(I) {}
public:
static Singleton* instance() {return &single;}
int val() { return i; }
};
Singleton Singleton::single(11);
main() {
//Singleton x(1); // error can't create a Singleton
// it is possible to access the single instance
cout << Singleton::instance()->val
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How about a namespace instead of a class?
wormzer,
How would the namespace help?
BeyondWu,
Your first method is not very practical.
The following would crash.
x Myx,Myx2;
Myx2.MyFunction();
Second line would crash. No way of knowing if Myx2 deleted itself.
Samething with the following:
x *Myx,*Myx2 = new x;
Myx =new x;
Myx->MyFunction();
Crash again.
How would the namespace help?
BeyondWu,
Your first method is not very practical.
The following would crash.
x Myx,Myx2;
Myx2.MyFunction();
Second line would crash. No way of knowing if Myx2 deleted itself.
Samething with the following:
x *Myx,*Myx2 = new x;
Myx =new x;
Myx->MyFunction();
Crash again.
This is what Bruce Eckel proposes:
#include <iostream>
using namespace std;
class Singleton {
static Singleton s;
int i;
Singleton(int x) : i(x) { }
void operator=(Singleton&);
Singleton(const Singleton&);
public:
static Singleton& getHandle() {
return s;
}
int getValue() { return i; }
void setValue(int x) { i = x; }
};
Singleton Singleton::s(47);
int main() {
Singleton& s = Singleton::getHandle();
cout << s.getValue() << endl;
Singleton& s2 = Singleton::getHandle();
s2.setValue(9);
cout << s.getValue() << endl;
}
There is further discussion about singleton in his book. Visit www.bruceeckel.com
#include <iostream>
using namespace std;
class Singleton {
static Singleton s;
int i;
Singleton(int x) : i(x) { }
void operator=(Singleton&);
Singleton(const Singleton&);
public:
static Singleton& getHandle() {
return s;
}
int getValue() { return i; }
void setValue(int x) { i = x; }
};
Singleton Singleton::s(47);
int main() {
Singleton& s = Singleton::getHandle();
cout << s.getValue() << endl;
Singleton& s2 = Singleton::getHandle();
s2.setValue(9);
cout << s.getValue() << endl;
}
There is further discussion about singleton in his book. Visit www.bruceeckel.com
proskig,
Isn't that the same thing I posted up above.
sateesh_babu was first to give the Singleton suggestion.
Isn't that the same thing I posted up above.
sateesh_babu was first to give the Singleton suggestion.
The idea is the same, I do not insist on points ;-)
ASKER
very good idea , to tackle the problem
programatically , others effort are also praisewotrthy as most of them share the idea of singleton class.
thanks beyondwu
programatically , others effort are also praisewotrthy as most of them share the idea of singleton class.
thanks beyondwu
But why only grade B ;-).
ASKER
your idea is good but the singleton class
solution by others is a also a potential solutions to this problem.again why you are worried about grade,it makes no difference.
saumendra
solution by others is a also a potential solutions to this problem.again why you are worried about grade,it makes no difference.
saumendra
1) Make all members of your class static, so that any instance of the class references the same set of data.
2) Make constructors of the class protected or private, so that nobody from the outside world can create an instance of the class. Then provide a (static) member function to create an instance of the class. In this function you can check if it is a first instance being created, and, if not, simply refuse the request.