Instance() in Singleton Class Question

I

Immortal Nephi

I am curious to ask. Why do you need to use Instance() function?
You can always use Singleton() function and ~Singleton() function.
Execute Instance() function will start to execute Singleton() function
before it returns back to Instance() function. The program exited
before it will not execute ~Singleton() function. You must use
Terminate() function manually to clear dynamic memory otherwise leak
memory can occur. It is not worth practice.
Read Note 1 and Note 2 inside main() function. Note 2 does its own
job to execute ~Singleton() function before program exits. Note 2 is
always useful when you want to use only one instance (one object such
as keyboard, mouse, floppy drive, etc)...

#include <stdio.h>
#include <stdlib.h>

class Singleton
{
public:
static Singleton* Instance(); // Why do you need it?
void Terminate(); // Why do you need it?

static void Print();

//protected:
Singleton();
~Singleton();


private:
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);

static Singleton* pinstance; // Why do you need it?
};

Singleton* Singleton::pinstance = 0;

Singleton* Singleton::Instance ()
{
if (pinstance == 0)
pinstance = new Singleton;

return pinstance;
}

void Singleton::Terminate ()
{
if (pinstance != 0)
{
delete pinstance;
pinstance = 0;
}
}

Singleton::Singleton()
{
printf("Constructor\n");
}

Singleton::~Singleton()
{
printf("Destructor\n");
}

void Singleton::print()
{
printf("Test\n");
}

int main()
{
// Note 1
Singleton & ref = * Singleton::Instance();
ref.Print();
ref.Terminate();


// Note 2
Singleton s;
s.Print(); // ( Singleton::print() is same as s.Print() )

system("pause"); // Function comes from Microsoft Visual C++ .Net

return 0;
}
 
V

Vladimir Jovic

Immortal said:
I am curious to ask. Why do you need to use Instance() function?
You can always use Singleton() function and ~Singleton() function.

The constructor and destructor are protected, therefore no access to them
Execute Instance() function will start to execute Singleton() function
before it returns back to Instance() function. The program exited
before it will not execute ~Singleton() function. You must use
Terminate() function manually to clear dynamic memory otherwise leak
memory can occur. It is not worth practice.

Not really. Normal OS will clean up after finished process.
Read Note 1 and Note 2 inside main() function. Note 2 does its own
job to execute ~Singleton() function before program exits. Note 2 is
always useful when you want to use only one instance (one object such
as keyboard, mouse, floppy drive, etc)...

#include <stdio.h>
#include <stdlib.h>

class Singleton
{
public:
static Singleton* Instance(); // Why do you need it?

To get the pointer to the only instance of this object.
void Terminate(); // Why do you need it?

This method is not needed
static void Print();

//protected:
Singleton();
~Singleton();


private:
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);

static Singleton* pinstance; // Why do you need it?
};

Singleton* Singleton::pinstance = 0;

Singleton* Singleton::Instance ()
{
if (pinstance == 0)
pinstance = new Singleton;

return pinstance;
}

void Singleton::Terminate ()
{
if (pinstance != 0)
{
delete pinstance;
pinstance = 0;
}
}

Singleton::Singleton()
{
printf("Constructor\n");
}

Singleton::~Singleton()
{
printf("Destructor\n");
}

void Singleton::print()
{
printf("Test\n");
}

int main()
{
// Note 1
Singleton & ref = * Singleton::Instance();
ref.Print();
ref.Terminate();


// Note 2
Singleton s;

Don't you get an error? The constructor is protected
 
S

Salt_Peter

I am curious to ask. Why do you need to use Instance() function?
You can always use Singleton() function and ~Singleton() function.

Assuming the ctor/dtor isn't protected, that would allow mutiple
instances of a Singleton.
Isn't it the programmer's responsability that this not occur in this
case? Or was the plan to trust the 'users of the class' to never
invoke that constructor more than once?
Execute Instance() function will start to execute Singleton() function
before it returns back to Instance() function. The program exited
before it will not execute ~Singleton() function. You must use
Terminate() function manually to clear dynamic memory otherwise leak
memory can occur. It is not worth practice.

The fix is to not allocate on the heap at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top