Why can I call a SingeltonObjects non static member functions without ever calling its constructor?

CDoubleHashing.h

1
2
3
4
5
6
7
8
9
10
11
class CDoubleHashing { 
public: 
static CDoubleHashing& getInstance(); 
virtual ~CDoubleHashing(); 
int hash(...); 
private: 
CDoubleHashing(); 
CDoubleHashing(CDoubleHashing const& other); 
CDoubleHashing operator=(CDoubleHashing& other); 
static CDoubleHashing* m_pInstance; 
}; 



And

CDoubleHashing.cpp
1
2
3
4
5
6
7
8
9
10
CDoubleHashing* CDoubleHashing::m_pInstance = NULL; 

CDoubleHashing& CDoubleHashing::getInstance() {return *m_pInstance;} 

CDoubleHashing::~CDoubleHashing(){} 

CDoubleHashing::CDoubleHashing(){ 
std::cout <<"HELLO!"<< std::endl;} 

int CDoubleHashing::hash(...){	...} 



and in my main function:
1
2
3
4
5
6
int main(int argc, char** argv) 
{ 
CDoubleHashing& hash = CDoubleHashing::getInstance(); 
std::cout << hash.hash(1,4,3,4); 
return 0; 
} 




The output to this is just:
2

which is determined by the hash function etc. "Hello!" is never printed

Now my question is why is the "HELLO!" never printed to consol? I use the hash function of a CDoubleHashing object, which was never constructed. How is that possible.
And how is this code conceptually different from one where I had made the followign changes:

1
2
3
4
5
CDoubleHashing& CDoubleHashing::getInstance(){ 
if(m_pInstance == NULL) 
m_pInstance = new m_pInstance; 
return m_pInstance; 
} 


and calling uppon it not as
CDoubleHashing& hash = CDoubleHashing::getInstance();
hash.hash(...);

but as
CDoubleHashing::getInstance()->hash(...)

destructor would need changes too i guess
The reason is because it was never constructed. Notice, though, that you are deferencing a null pointer, so it could well crash. The reason why it might not is because your class has no state - the hash function is presumably already in memory somewhere, and all you are doing is calling it. If it modified state then it would be another story...

As for why it is different, is because in your second example you construct the instance first before sending it. Also, your two callings of the function are actually identical, but one is split over two lines and the other isn't.

On a slightly unrelated note, this doesn't seem to be a good time for a Singleton pattern. Because your hash function does not access (or modify) state, there is no need for it to be part of a class. Rather, it should be a normal function. If you feel like it HAS to be in a class for some reason, it should be something like this:
1
2
3
4
class CDoubleHashing {
    public:
        static int hash(...) { ... }
};

Though, unless there are other reasons to do with your code base you have, I can't think of a reason as to why you would need to do that...
Topic archived. No new replies allowed.