I've seen codes for singletons in which people write =, copy
constructor, constructor in private section and have some kind of
static member function which returns instance of class. I was able to
understand that.
But today I came across this code which I saw in "Test Your C++ Skills"
by Yashwant Kanetkar. This looks even more simpler but like many other
questions in the book I am sure this must also be having some problems.
I modified the code to add standard headers and changed return type of
main to int.
#include<cstdlib>
#include<iostream>
using namespace std;
class sample{
private:
static int count;
public:
sample(){
if(1==count)
exit(0);
cout<<"Object created\n";
++count;
}
};
int sample::count=0;
int main(){
sample s1;
sample s2;
return 0;
}
Can someone say few points about this?