pimpl idiom and singletons

Discussion in 'C++' started by Noah Roberts, May 24, 2007.

  1. Noah Roberts

    Noah Roberts Guest

    Some little tidbit I just ran into that might help some, especially
    novice programmers.

    If you are using the pimpl idiom, as you probably should be most of the
    time, then it is very straightforward to turn your class into a
    singleton object. Consider:

    class X
    {
    struct impl;
    boost::scoped_ptr<impl> pimpl;

    public:
    X();
    ~X();

    void f();
    }

    X::X() : pimpl(new impl){}
    X::~X() {}
    void X::f() { pimpl->f(); }

    To turn that into a singleton just do the following:

    1. turn pimpl into a static member.
    2. Initialize it in the cpp file as usual.
    3. you're done.

    You could change it so that the constructor is private and you have an
    instance() function but why bother?

    X x;
    x->f();

    Creating the x object is of minimal expense. You may later decide a
    singleton isn't a good way to solve the problem. None of your client
    code would need to be changed.

    There may be some extra bookkeeping to do of course so you might need
    some initializer function that is called once. This is pretty easy to
    do and can be just built into or called from the constructor.
    Noah Roberts, May 24, 2007
    #1
    1. Advertising

  2. Noah Roberts

    BobR Guest

    Noah Roberts <> wrote in message ...
    > Some little tidbit I just ran into that might help some, especially
    > novice programmers.
    >
    > If you are using the pimpl idiom,


    If you are trying to help 'newbies', you might want to tell what 'pimpl' is
    so they don't go popping zits.

    --
    Bob <G> R
    POVrookie
    BobR, May 24, 2007
    #2
    1. Advertising

  3. Noah Roberts

    Noah Roberts Guest

    BobR wrote:
    > Noah Roberts <> wrote in message ...
    >> Some little tidbit I just ran into that might help some, especially
    >> novice programmers.
    >>
    >> If you are using the pimpl idiom,

    >
    > If you are trying to help 'newbies', you might want to tell what 'pimpl' is
    > so they don't go popping zits.


    That's easily looked up in google. I can't think of any place where
    what I'm describing is on the net though, certainly not as simple as
    typing something into a search field.
    Noah Roberts, May 25, 2007
    #3
  4. On May 24, 3:14 pm, Noah Roberts <> wrote:
    > Some little tidbit I just ran into that might help some, especially
    > novice programmers.
    >
    > If you are using the pimpl idiom, as you probably should be most of the
    > time, then it is very straightforward to turn your class into a
    > singleton object. Consider:
    >
    > class X
    > {
    > struct impl;
    > boost::scoped_ptr<impl> pimpl;
    >
    > public:
    > X();
    > ~X();
    >
    > void f();
    >
    > }
    >
    > X::X() : pimpl(new impl){}
    > X::~X() {}
    > void X::f() { pimpl->f(); }
    >
    > To turn that into a singleton just do the following:
    >
    > 1. turn pimpl into a static member.
    > 2. Initialize it in the cpp file as usual.
    > 3. you're done.
    >
    > You could change it so that the constructor is private and you have an
    > instance() function but why bother?
    >
    > X x;
    > x->f();
    >
    > Creating the x object is of minimal expense. You may later decide a
    > singleton isn't a good way to solve the problem. None of your client
    > code would need to be changed.
    >
    > There may be some extra bookkeeping to do of course so you might need
    > some initializer function that is called once. This is pretty easy to
    > do and can be just built into or called from the constructor.


    nonsense! pimpls and singletons are orthogonal
    consider the case of the 'impl' having data/state

    Diego
    Diego Martins, May 25, 2007
    #4
  5. Noah Roberts

    Noah Roberts Guest

    Diego Martins wrote:

    > nonsense! pimpls and singletons are orthogonal
    > consider the case of the 'impl' having data/state


    What makes you think that singletons don't have those things??
    Noah Roberts, May 25, 2007
    #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. Icosahedron

    Pimpl Idiom

    Icosahedron, Nov 20, 2003, in forum: C++
    Replies:
    7
    Views:
    701
    Icosahedron
    Nov 22, 2003
  2. Debajit  Adhikary
    Replies:
    2
    Views:
    2,035
    Christopher Benson-Manica
    Jul 15, 2004
  3. Peteris Krumins
    Replies:
    2
    Views:
    487
    Peteris Krumins
    Aug 31, 2005
  4. jimmy

    friendship and pImpl idiom

    jimmy, Feb 8, 2006, in forum: C++
    Replies:
    3
    Views:
    441
    Alf P. Steinbach
    Feb 9, 2006
  5. Daniel Lidström
    Replies:
    15
    Views:
    638
    Brendon Costa
    Oct 31, 2007
Loading...

Share This Page