Singleton class

The code below is a singleton class called base.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
  2 
  3 
  4 using namespace std;
  5 
  6 
  7 class base
  8 {
  9 private:
 10         base();
 11         ~base();
 12         static int total;
 13         static base *ptr;
 14         static bool instance;
 15 public:
 16         static base *getInstance();
 17         void set(){total++;}
 18         int get(){return total;}
 19 
 20 };
 21 
 22 bool base::instance = false;
 23 base* base::ptr;
 24 int base::total = 0;
 25 
 26 base *base::getInstance()
 27 {
 28         if(!instance)
 29         {
 30                 ptr = new base;
 31                 instance = true;
 32         }
 33 
 34         return ptr;
 35 }
 36 
 37 base::base()
 38 {
 39         cout<<"im in constructor"<<endl;
 40         total++;
 41 
 42 }
 43 
 44 base::~base()
 45 {
 46         cout<<"im in destructor"<<endl;
 47         delete ptr;
 48 
 49 }
 50 
 51 int main()
 52 {
 53 base* p = base::getInstance();
 54 p->set();
 55 cout<<p->get()<<endl;

 58 return 0;
 59 }


im in constructor
2



Why the destructor is not called even though the object is created dynamically?
That's because the object is never destroyed.
But should it not be that the object be destroyed at the end of the program after main() exits. I tried the code with a little modification on static variables and the destructor is called but whereas here it is not. reason?

But should it not be that the object be destroyed at the end of the program after main() exits.


No. It is your responsibility to delete whatever you new, if you don't, that's your fault and you just created a memory leak.
Hi ,
I have similar problem .

Its giving me error


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

class CSingleton
{
public:
	static CSingleton& GetSingleton();
	void DoSomething(); 
	~CSingleton();
private:
	CSingleton();
	static CSingleton* instance ;
};

CSingleton* CSingleton::instance = 0;

CSingleton::CSingleton()
{
	cout<<"\n Singleton constructor ";
}
CSingleton::~CSingleton()
{
	cout<<"\n Singleton Destuctor ";
//	if(instance!= NULL)
//		delete instance ;
}

CSingleton& CSingleton::GetSingleton()
{
	cout<<"\n GetSingleton function of Singleton ";
	if( instance == NULL )
	{
		instance = new CSingleton();
	}
	return *instance ;
}

void CSingleton::DoSomething()
{
	cout<<"\n DoSomething of Singleton ";
}





int _tmain(int argc, _TCHAR* argv[])
{
	
	CSingleton sing =  CSingleton::GetSingleton();
	delete sing;
	
}


error C2440: 'delete' : cannot convert from 'CSingleton' to 'void *'


how do i solve it .
Thanks in advance
CSingleton sing

is not a pointer, make it CSingleton* sing
in GetSingleton(), dont return *instance you should return instance, and change the return type from reference to pointer

Or

you could make it so;
CSingleton* sing = &CSingleton::GetSingleton();
Last edited on
But should it not be that the object be destroyed at the end of the program after main() exits.
No. It is your responsibility to delete whatever you new, if you don't, that's your fault and you just created a memory leak.


Technically he is right, as the pointer ceases to exist when main ends. This does, however not apply to the memory pointed TO by the pointer.
Last edited on


Having done the above changes as suggested by Skillless but ,

Now it is giving me the error

error C2440: 'return' : cannot convert from 'CSingleton *' to 'CSingleton &'
Last edited on
Post the code..

Did you change the return type of GetSingleton to pointer instead of reference?
yes i did ..
is sing a pointer?

Again, post the code =p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

class CSingleton
{
public:
	static CSingleton& GetSingleton();
	void DoSomething(); 
	~CSingleton();
private:
	CSingleton();
	static CSingleton* instance ;
};

CSingleton* CSingleton::instance = 0;

CSingleton::CSingleton()
{
	cout<<"\n Singleton constructor ";
}
CSingleton::~CSingleton()
{
	cout<<"\n Singleton Destuctor ";
//	if(instance!= NULL)
//		delete instance ;
}

CSingleton& CSingleton::GetSingleton()
{
	cout<<"\n GetSingleton function of Singleton ";
	if( instance == NULL )
	{
		instance = new CSingleton();
	}
	return instance ;
}

void CSingleton::DoSomething()
{
	cout<<"\n DoSomething of Singleton ";
}


int _tmain(int argc, _TCHAR* argv[])
{
	
	CSingleton* sing =  &CSingleton::GetSingleton();
	delete sing;
	
}
Or doesnt mean And, you did option A, and B, and thats not going to work

Anyway;
Did you change the return type of GetSingleton to pointer instead of reference?
bluecoder wrote:
yes i did ..
CSingleton& CSingleton::GetSingleton()
Oh no you didn'

And change
CSingleton* sing = &CSingleton::GetSingleton();
to
CSingleton* sing = CSingleton::GetSingleton();
Last edited on
Sorry Skillless , first for not making the correct changes first .

And thanks , it worked .
Topic archived. No new replies allowed.