Instance() in Singleton Class Question

Discussion in 'C++' started by Immortal Nephi, Apr 13, 2009.

  1. 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;
    }
     
    Immortal Nephi, Apr 13, 2009
    #1
    1. Advertising

  2. Immortal Nephi wrote:
    > 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.Print(); // ( Singleton::print() is same as s.Print() )
    >
    > system("pause"); // Function comes from Microsoft Visual C++ .Net
    >
    > return 0;
    > }
     
    Vladimir Jovic, Apr 13, 2009
    #2
    1. Advertising

  3. Immortal Nephi

    Salt_Peter Guest

    On Apr 13, 1:15 am, Immortal Nephi <> wrote:
    > 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.

    > 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;
    >
    > }
     
    Salt_Peter, Apr 13, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,412
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Martin P. Hellwig
    Replies:
    1
    Views:
    377
    Martin P. Hellwig
    Mar 26, 2010
  3. Caleb Tennis
    Replies:
    3
    Views:
    163
  4. Paul McMahon
    Replies:
    3
    Views:
    207
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    204
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page