Topic: Singleton class


Author: "Robert O'Dowd" <nospam@nonexistant.com>
Date: 1999/09/24
Raw View
Mathieu Tanguay wrote:
>
> I have a class that I want to make singleton.
>
> class DTPalletManager : public DTBase
> {
> public:
>  DTPalletManager( );
>  ~DTPalletManager( );
>
> protected:
>  DTPalletManager* InqPtr();
>
> private:
>  static DTPalletManager* mThis;
>  DTPalletManager( const bool ab );
> };
>
> In the implementation I do this:
>
> #include "DTPalletManager.h"
>
> DTPalletManager* DTPalletManager::mThis = NULL;
>
> DTPalletManager::DTPalletManager( )
> {
>      if( !mThis )
>     {
>         DTBase();
>         mThis = new DTPalletManager(true);
>     }
> }
>
> DTPalletManager::DTPalletManager( const bool ab )
> {
> }
>
> I know that there is a method to do this whitout having to call the
> second constructor who's private but I can't remember.

What you have here isn't a singleton, because you can create
multiple instances of DTPalletManager.

Consider the code

main()
{
   DTPalletManager *x = new DTPalletManager;
   DTPalletManager *y = new DTPalletManager;
   DTPalletManager *z = new DTPalletManager;

   // whatever

   return 0;
}

x,y, and z will point to different objects.

One way of implementing a singleton is to make all constructors
and the destructor of the class private, and have class instantiation
and destruction managed through either friend functions or
public static member functions of the class.

>
> My second question is : Is there a way to force the constructor not to
> call the constructor of the class DTBase (inherited) unless I tell it
> so.
>

No.  A constructor of DTBase must be called.  Among other things,
failure
to do so would probably result in your class being in an invalid state.

The standard mandates that a constructor of DTBase must be called before
the constructor of DTPalletManager.  From derived classes, you can
control
*which* DTBase contructors are invoked.  If you don't specify which
constructor to call, the compiler invokes the default constructor of
DTBase (assuming such a beast exists).

A way to defer construction of DTBase is to use aggregation rather
than inheritence.  Say

class DTPalletManager
{
    private:
       DTBase *object;
    public:
       DTBase() {object = NULL;};

       void construct() { if (object == NULL) object = new DTBase;};
};



-<Automagically included trailer>
Robert O'Dowd                       Ph    +61 (8) 8259 6546
MOD/DSTO                     Fax    +61 (8) 8259 5139
P.O. Box 1500                       Email:
robert.odowd@dsto.defence.gov.au
Salisbury, South Australia, 5108

Disclaimer: Opinions above are mine and may be worth what you paid for
them
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Mathieu Tanguay <mtanguay@megatoon.ca>
Date: 1999/09/23
Raw View
I have a class that I want to make singleton.

class DTPalletManager : public DTBase
{
public:
 DTPalletManager( );
 ~DTPalletManager( );

protected:
 DTPalletManager* InqPtr();

private:
 static DTPalletManager* mThis;
 DTPalletManager( const bool ab );
};


In the implementation I do this:

#include "DTPalletManager.h"

DTPalletManager* DTPalletManager::mThis = NULL;

DTPalletManager::DTPalletManager( )
{
     if( !mThis )
    {
        DTBase();
        mThis = new DTPalletManager(true);
    }
}

DTPalletManager::DTPalletManager( const bool ab )
{
}


I know that there is a method to do this whitout having to call the
second constructor who's private but I can't remember.

My second question is : Is there a way to force the constructor not to
call the constructor of the class DTBase (inherited) unless I tell it
so.

Thanks
Mathieu Tanguay



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]