singleton template class

Discussion in 'C++' started by puzzlecracker, Aug 28, 2008.

  1. Will this function is singleton or do I need to declare a ctor in the
    derived class as public, as well as copy-ctor and assignment
    operator.

    template<typename T>
    class CSingleton
    {
    public:
    static T& Instance()
    {
    static T me;
    return me;
    }
    };

    class MyClass: public CSingleton<MyClass>
    {
    public:
    MyClass(){};
    ~MyClass(){};
    void Print() { printf("testing %d\n",val); }
    int val;
    };
    puzzlecracker, Aug 28, 2008
    #1
    1. Advertising

  2. puzzlecracker

    Guest

    On 28 Aug, 18:12, puzzlecracker <> wrote:
    > Will this function is singleton or do I need to declare a ctor in the
    > derived class as public, as well as copy-ctor and assignment
    > operator.
    >
    > template<typename T>
    > class CSingleton
    > {
    >  public:
    >    static T& Instance()
    >    {
    >      static T me;
    >      return me;
    > }
    >  };
    >
    >  class MyClass: public CSingleton<MyClass>
    >  {
    >    public:
    >      MyClass(){};
    >      ~MyClass(){};
    >      void Print() { printf("testing %d\n",val); }
    >      int val;
    >  };


    Did you mean make the ctor in the derived class PRIVATE, as well as
    copy-ctor and assignment?

    As your class stands, there's nothing to stop you from instantiating
    an instance of MyClass directly. Or inadvertently copying the class.
    Making the copy-ctor and assignment operator private would stop people
    coding

    MyClass copyOfMyClass = MyClass::Instance();

    when they meant

    MyClass& copyOfMyClass = MyClass::Instance();

    But there's no neat way to hide the constructor, to prevent its direct
    use. You could do something like this (inc. making CSingleton<> a
    friend).

    class MyClass: public CSingleton<MyClass>
    {
    public:
    ~MyClass(){}

    void Print()
    {
    printf("testing %d (%x)\n", val, (int)this);
    }

    int val;

    private:
    // stop direct construction
    MyClass():val(0){}

    // stop copy construction and copying
    MyClass(const MyClass&);
    MyClass& operator=(const MyClass&);

    // allow CSingleton<> to construct us
    friend CSingleton<MyClass>;
    };

    It is more common to return a pointer from the Instance() method. This
    helps with the copy-ctor/assignment related problems.

    Also, you might be interested in the thread "Problem with Singleton
    template" posted to comp.lang.c++.moderated on 20 Aug. this year
    (2008, for people Googling in the future).

    Andy
    , Aug 30, 2008
    #2
    1. Advertising

  3. puzzlecracker

    cch@srdgame Guest

    于 Thu, 28 Aug 2008 10:12:24 -0700,puzzlecracker写到:

    > Will this function is singleton or do I need to declare a ctor in the
    > derived class as public, as well as copy-ctor and assignment operator.
    >
    > template<typename T>
    > class CSingleton
    > {
    > public:
    > static T& Instance()
    > {
    > static T me;
    > return me;
    > }
    > };
    >
    > class MyClass: public CSingleton<MyClass> {
    > public:
    > MyClass(){};
    > ~MyClass(){};
    > void Print() { printf("testing %d\n",val); } int val;
    > };


    Following code is one reference for you:
    /
    **============================================================================
    * Copyright (C) 2006 Team RioWow
    *
    * File: singleton.h
    * Description:
    This file is to define and surpport different complier and system.
    * Author: Rio Chang
    * Update: 10/28/2006
    =============================================================================*/
    #ifndef SINGLETON_H
    #define SINGLETON_H

    template <class T>
    class Singleton
    {
    public:
    static T* getInstance()
    {
    return &(getSingleton());
    }
    static T& getSingleton()
    {
    static T instance;
    return instance;
    }
    ~Singleton(){};
    protected:
    Singleton(const Singleton& sig);
    Singleton& operator = (const Singleton& sig);
    Singleton(){};
    }
    ;
    #endif
    //------------------------------------------------------------------------------
    cch@srdgame, Aug 31, 2008
    #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. christopher diggins
    Replies:
    16
    Views:
    733
    Pete Becker
    May 4, 2005
  2. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,218
    Proton Projects - Moin
    Mar 27, 2007
  3. A L
    Replies:
    1
    Views:
    503
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  4. Paul McMahon
    Replies:
    3
    Views:
    196
    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:
    196
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page