how to replace function-scoped static singleton

Discussion in 'C++' started by Bob Doe, Nov 19, 2008.

  1. Bob Doe

    Bob Doe Guest

    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;
    }
     
    Bob Doe, Nov 19, 2008
    #1
    1. Advertising

  2. Bob Doe

    James Kanze Guest

    On Nov 19, 10:25 pm, Paavo Helde <> wrote:
    > Bob Doe <> kirjutas:
    >
    > > 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
     
    James Kanze, Nov 20, 2008
    #2
    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. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,337
    Proton Projects - Moin
    Mar 27, 2007
  2. requinham
    Replies:
    11
    Views:
    2,371
    Michael Doubez
    Feb 19, 2010
  3. Wilhelm
    Replies:
    1
    Views:
    187
  4. Trans
    Replies:
    12
    Views:
    305
    Robert Klemme
    Sep 14, 2007
  5. Paul McMahon
    Replies:
    3
    Views:
    224
    David A. Black
    Jun 9, 2008
Loading...

Share This Page