how to replace function-scoped static singleton

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Doe

    how to replace function-scoped static singleton

    Hello,

    how to I replace singleton classes using function scope static
    variables with one that doesn't use function scope static variables?:

    class Foo {
    public:
    static Foo &instance();
    virtual ~Foo();
    ...
    private:
    Foo();
    Foo(const Foo&);
    Foo & operator=(const Foo&);
    };

    ----------------------------------
    Foo &Foo::instance( )
    {
    static Foo& theInstance;
    ...
    return theInstance;
    }


  • Victor Bazarov

    #2
    Re: how to replace function-scoped static singleton

    Bob Doe wrote:
    how to I replace singleton classes using function scope static
    variables with one that doesn't use function scope static variables?:
    What is the problem? What are you trying to replace it with?
    Anything in particular? "Not using function scope" is not much
    of a specification. Have you looked at possible implementations
    of the Singleton pattern? Try googling it. Try looking in some
    smart books (like the GoF one). Try looking in the archives.

    This has been discussed so many times that it just doesn't need
    to be repeated, honestly.
    >
    class Foo {
    public:
    static Foo &instance();
    virtual ~Foo();
    ...
    private:
    Foo();
    Foo(const Foo&);
    Foo & operator=(const Foo&);
    };
    >
    ----------------------------------
    Foo &Foo::instance( )
    {
    static Foo& theInstance;
    ...
    return theInstance;
    }
    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Paavo Helde

      #3
      Re: how to replace function-scoped static singleton

      Bob Doe <DumpForJunk@gm ail.comkirjutas :
      Hello,
      >
      how to I replace singleton classes using function scope static
      variables with one that doesn't use function scope static variables?:
      >
      class Foo {
      public:
      static Foo &instance();
      virtual ~Foo();
      Not much point to have public virtual dtor for a singleton object which
      is never destroyed ;-) But it does not hurt, of course.
      ...
      private:
      Foo();
      Foo(const Foo&);
      Foo & operator=(const Foo&);
      };
      >
      ----------------------------------
      Foo &Foo::instance( )
      {
      static Foo& theInstance;
      This won't compile.

      Function scope static variables are a proven method for creating
      singletons. If this does not work for you, you should provide some
      explanation about your worries (e.g. multithreading concerns, memory leak
      alarms, etc...)

      Paavo



      Comment

      • James Kanze

        #4
        Re: how to replace function-scoped static singleton

        On Nov 19, 10:25 pm, Paavo Helde <pa...@nospam.p lease.orgwrote:
        Bob Doe <DumpForJ...@gm ail.comkirjutas :
        >
        Hello,
        >
        how to I replace singleton classes using function scope static
        variables with one that doesn't use function scope static variables?:
        >
        class Foo {
        public:
        static Foo &instance();
        virtual ~Foo();
        >
        Not much point to have public virtual dtor for a singleton object which
        is never destroyed ;-) But it does not hurt, of course.
        >
        ...
        private:
        Foo();
        Foo(const Foo&);
        Foo & operator=(const Foo&);
        };
        >
        ----------------------------------
        Foo &Foo::instance( )
        {
        static Foo& theInstance;
        >
        This won't compile.
        >
        Function scope static variables are a proven method for creating
        singletons. If this does not work for you, you should provide some
        explanation about your worries (e.g. multithreading concerns, memory leak
        alarms, etc...)
        >
        Paavo

        Comment

        Working...