singleton base class

Discussion in 'C++' started by Anthony Lansbergen, Jul 1, 2003.

  1. Hi,
    I want to create an abstract base class for singletons.
    The base class should demand that the derived classes implement a static
    instance member operation.
    Is it possible to create a pure virtual static member function in a base
    classe?
    If not, is there an better elegant solution for this problem?

    Thanks in advance,
    Anthony Lansbergen


    --
    to reply e-mail change EdAsys into adesys
     
    Anthony Lansbergen, Jul 1, 2003
    #1
    1. Advertising

  2. "Anthony Lansbergen" <> wrote in news:3f015d4a$0$49099
    $4all.nl:

    > Hi,
    > I want to create an abstract base class for singletons.
    > The base class should demand that the derived classes implement a static
    > instance member operation.
    > Is it possible to create a pure virtual static member function in a base
    > classe?
    > If not, is there an better elegant solution for this problem?


    You can't have a virtual static member. The member will be static to the
    base class, always. This caused me quite a few headaches until I realised
    what was going on; in the end, I had to use templates to get round it.

    Chris
    --
    Chris Whitworth * On XBL as parm * Playing Ghost Recon, MechAssault, MotoGP

    "back when I was young, we had to travel back in time to put the tape
    in so the game would load before we died."
    --otama, ua2
     
    Chris Whitworth, Jul 1, 2003
    #2
    1. Advertising

  3. "Anthony Lansbergen" <> wrote...
    > I want to create an abstract base class for singletons.
    > The base class should demand that the derived classes implement a static
    > instance member operation.
    > Is it possible to create a pure virtual static member function in a base
    > classe?


    No. "virtual" and "static" contradict each other.

    > If not, is there an better elegant solution for this problem?


    Think templates. If you define the entire class or some member
    function to use the "instance" member function of the template
    argument, then it will work as if you had that requirement for
    a "virtual static".

    template<class Derived> class MySingleton {
    ...
    Derived::instance()
    ...
    };

    And the user of your template would do

    class MySpecialSingleton : MySingleton<MySpecialSingleton> {
    ...
    };

    Victor
     
    Victor Bazarov, Jul 1, 2003
    #3
  4. Anthony Lansbergen

    Chris Theis Guest

    "Anthony Lansbergen" <> wrote in message
    news:3f015d4a$0$49099$4all.nl...
    > Hi,
    > I want to create an abstract base class for singletons.
    > The base class should demand that the derived classes implement a static
    > instance member operation.
    > Is it possible to create a pure virtual static member function in a base
    > classe?
    > If not, is there an better elegant solution for this problem?
    >


    I'd recommend to use a templated wrapper for your singletons. Look at the
    approaches in Modern Design C++ or you can use the following simple
    solution:

    template<class T, class Tag = void>
    class CSingleton
    {
    private:
    CSingleton(void) { }
    ~CSingleton(void) { }
    CSingleton( const CSingleton& rhs );
    CSingleton& operator=( const CSingleton& rhs);
    public:
    static T& Instance() {
    static T m_Instance;
    return m_Instance;
    }
    };

    An example class which should become a "singleton" would for example look
    like this:

    class CObj {
    protected:
    CObj() {};
    CObj& operator=( const CObj& rhs);
    CObj( const CObj& rhs );

    public:
    ~CObj() {};
    friend class CSingleton<CObj>;
    int m_i;
    };

    The usage would be like this:

    CObj& MyObj = CSingleton<CObj>::Instance();

    HTH
    Chris
     
    Chris Theis, Jul 1, 2003
    #4
  5. Anthony Lansbergen

    Evan Guest

    "Chris Theis" <> wrote in message news:<bdsadl$p96$>...
    > "Anthony Lansbergen" <> wrote in message
    > news:3f015d4a$0$49099$4all.nl...
    > > Hi,
    > > I want to create an abstract base class for singletons.
    > > The base class should demand that the derived classes implement a static
    > > instance member operation.
    > > Is it possible to create a pure virtual static member function in a base
    > > classe?
    > > If not, is there an better elegant solution for this problem?
    > >

    >
    > I'd recommend to use a templated wrapper for your singletons. Look at the
    > approaches in Modern Design C++ [snip]


    Or download said approaches from
    http://www.awl.com/cseng/titles/0-201-70431-5, though they very well
    may not make total sense without reading the book first. I can't
    remember if it uses template partial specalization, but if so, you'll
    also neeed a really recent compiler to use.
     
    Evan, Jul 2, 2003
    #5
    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. Alf P. Steinbach
    Replies:
    6
    Views:
    549
    John Carson
    Sep 3, 2005
  2. Hicham Mouline
    Replies:
    1
    Views:
    596
    Victor Bazarov
    Apr 20, 2009
  3. Paul McMahon
    Replies:
    3
    Views:
    207
    David A. Black
    Jun 9, 2008
  4. 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
  5. Karan Rajput
    Replies:
    2
    Views:
    146
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page