Without constructor, how comes destructor?

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
59
#include <iostream>


using namespace std;

class Singleton
{
public:
	static Singleton GetSingleton();
	void DoSomething();
	~Singleton();

private:
	Singleton();		
	static Singleton* _instance;
};

Singleton* Singleton::_instance = 0;// initialize pointer
//*********************************************************************************************
Singleton::Singleton()
{	

	std::cout << "Singleton::Singleton" << std::endl;
}

//*********************************************************************************************
Singleton::~Singleton()
{
	if(this)
		cout<<"fsf"<<endl;
	std::cout << "Singleton::~Singleton" << std::endl;
	//if(_instance)
	//	delete _instance;	
}

//*********************************************************************************************
Singleton Singleton::GetSingleton()
{
	std::cout << "Singleton::GetSingleton" << std::endl;
	if(_instance == NULL)
	{
		//_instance = new Singleton();
	}
	return *_instance;
}

//*********************************************************************************************
void Singleton::DoSomething()
{
	std::cout << "Singleton::DoSomething" << std::endl;
}


int main()
{
	Singleton::GetSingleton().DoSomething();
	system("pause");
    return 0;
}


1
2
3
4
5
6
//My output:
Singleton::GetSingleton
Singleton::DoSomething
fsf
Singleton::~Singleton
Press any key to continue . . .


I don't know why? Can anyone help?
Thanks very much!
Singleton Singleton::GetSingleton() returns a new copy of Singleton created with the default constructor.

You probably mean Singleton* Singleton::GetSingleton()
Singleton Singleton::GetSingleton() returns a new copy of Singleton created with the default constructor.

As you can see from the output the default constructor is never called. return *_instance; dereferences a garbage pointer null pointer, and since Singleton is an empty class (has no non-static variables) the destructor has nothing to do with it's implicit this pointer, therefore it doesn't crash, but if you add a member variable to Singleton, then the destructor will try to dereference the this pointer, which will fail, and the program will crash.
( I hope this is what happening :-) )

EDIT : just noticed the Singleton* Singleton::_instance = 0;// initialize pointer , so it dereferences a null pointer
Last edited on
closed account (z05DSL3A)
As kbw said, it is using a copy of Singleton, but it uses the copy constructor (not the default constructor) to make the copy which is why you do not see the default constructor output.


Still, I think Singleton::GetSingleton() returns a non fully valid object, since it crashes if I add a member variable.
Well line 42 is commented out, so you are never actually constructing a Singleton object.
closed account (z05DSL3A)
...so you are never actually constructing a Singleton object.


I think that was the point of the OPs question, but GetSingleton() returns a Singleton via the auto generated copy constructor. A perfectly valid Singleton object can be constructed when GetSingleton() returns *_instance (even with _instance being NULL) because there is no member data to copy. If you add member data to the Singleton the copy constructor will fail because it has been asked to copy an object at the NULL address.
Topic archived. No new replies allowed.